I always assume, as they said here http://en.wikipedia.org/wiki/Data_structure_alignment, “It is important to note that the last member is padded with the number of bytes required so that the total size of the structure should be a multiple of the largest alignment of any structure member”
So for the struct like this, its size should be 16 at a 32 processor
typedef struct
{
double s; /* 8 bytes */
char c; /* 7 bytes padding at the end of make the total size 8*2 */
} structa_t;
So I was quite surprised to the size is 12 instead of 16!! Why is that ? Can someone cast some light on it ?
sizeof(double) = 8
sizeof(structa_t) = 12
BTW, so system info
$ uname -a
Linux 2.6.18-8.el5xen #1 SMP Thu Mar 15 21:02:53 EDT 2007 i686 i686 i386 GNU/Linux
$ gcc --version
gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52)
The key wording here is:
On your system, the aligment of a
doubleis 4, not 8.If you wait for C1x, you can use the
_Alignofoperator (similar tosizeof). On your system,You can test the alignment in a more primitive way in C89,
Or with a macro,