dimanche 27 mars 2016

Union Bit Fields

I read http://ift.tt/1niiOJM:

The union is only as big as necessary to hold its largest data member. The other data members are allocated in the same bytes as part of that largest member. The details of that allocation are implementation-defined, and it's undefined behavior to read from the member of the union that wasn't most recently written.

I wonder why "it's undefined behavior to read from the member of the union that wasn't most recently written".

I wonder if the union has padding to enforce the order bits, will reading the member of the union that wasn't most recently written have defined behavior?

Example:

union {
uint8_t raw;
uint8_t a : 1;
uint8_t b : 2;
uint8_t padding : 5;
} U;

Will it be okay to read U.raw correctly?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire