#define MAX_SIZE 8
enum my_enum
{
VAL1 = 0;
VAL2,
VAL3,
VAL4,
VAL_MAX
};
struct my_struct
{
enum my_enum e;
int w[MAX_SIZE];
};
Can a layout in such structure cause alignment issues on a target platform? I understand it much depends on a platform, but generally C compiler is allowed to do padding of structures, so for example on 32 bit machine, where ‘int’ is 32 bit long:
struct my_struct
{
int w[MAX_SIZE];
}
is aligned (as fas as I understand) so compiler probably won’t be doing anything else with its layout, but adding ‘enum my_enum’ in the structure can poptentially get the structure un-aligned on such machine. Should I be doing anything special to avoid this and should I be avoiding it at all ?
Would be very thankful for clarifications!
Mark
The answer is no, you don’t have to do anything. If adding a field will break alignment, the compiler will apply padding in the appropriate places to realign it.
In your case,
enums are likely to be implemented as anintso you wouldn’t have this problem in the first place.A better example would be:
In this case, the compiler will likely place 3 bytes of padding after
chto keepwaligned to 4 bytes.