Consider the following union:
typedef union {
struct { // Anonymous struct
int red;
int green;
int blue;
};
int colorChannels[3];
} Color;
Regardless of the system this code is compiled or executed on, will the fields in the anonymous struct always line up with the indexes in colorChannels?
In other words, does the specification require the memory address of myColor.colorChannels[0]
to be the same as the address of myColor.red
?
Additionally, would the answer be different if the code was compiled as C++ rather than C?
Please note that I'm asking about the specific case where every element in the union has the same type/size (i.e. int
in this case)
VARIABLE ALIGNMENT specifies the alignment of data values in the Data Editor. It has no effect on the format of the variables or the display of the variables or values in other windows or printed results.
CServer Side ProgrammingProgramming. A union is a memory location that is shared by several variables of different data types in C programming language.
C unions allow data members which are mutually exclusive to share the same memory. This is quite important when memory is valuable, such as in embedded systems. Unions are mostly used in embedded programming where direct access to the memory is needed.
Regardless of the system this code is compiled or executed on, will the fields in the anonymous struct always line up with the indexes in colorChannels?
Not necessarily. The implementation can add byte "padding" between individual variables of the struct
. "Padding" is just additional bytes of memory inserted into the memory definition between variables of or at the end of the struct
in an implementation-defined way. If this happened, then your integer array would not be aligned with the memory layout of the struct
(unless the padding was only at the end of the struct
, maybe). The order of the variables in the struct
in memory should be consistent across implementations (in that variable a
will be followed by b
followed by c
in memory), but, again, the byte padding between can still be there (e.g., a
is followed by b
in memory but there is padding between a
and b
such that they are not right after each other in memory).
For some compilers like gcc, there are ways to modify how it handles padding. This could help guarantee the struct
would be in memory alignment with your integer array, but this could cause other memory problems downstream.
In other words, does the specification require the memory address of myColor.colorChannels[0] to be the same as the address of myColor.red?
If there is no padding between red
, green
, and blue
of the struct
, then the indexes of colorChannels
would match up with each variable in memory.
The C Standard doesn't make any guarantees about this, but it is likely to be correct on any actual system that exists.
I would suggest adding a compile-time check to the code so that in case there is some system that adds padding, you get a compilation error which you can deal with at the time:
_Static_assert( sizeof(Color) == 3 * sizeof(int), "We're on a weird system boys." );
NB. _Static_assert
was added in C11, prior to that you can use some hack as described here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With