Following the discussion from this post, I have understood that the main reason for the alignment of structure members is performance (and some architectures restrictions).
If we will investigate Microsoft (Visual C++), Borland/CodeGear (C++-Builder), Digital Mars (DMC) and GNU (GCC) when compiling for 32-bit x86: The alignment for int
is 4 bytes and if int
is not aligned, it can happen that 2 rows of memory banks will be read.
My question is why not to make double
to be 4 bytes aligned also? 4 bytes aligned double
also will cause 2 rows of memory banks reading....
For example in the following example, since double
is 8-aligned, the actual size of structure will be sizeof(char) + (alignment for double padding) + sizeof(int) = 20 bytes
.
typedef struct structc_tag{
char c;
double d;
int s;
} structc_t;
Thank you
See Question&Answers more detail:os