Tutorial by Examples

By default structures are padded in C. If you want to avoid this behaviour, you have to explicitly request it. Under GCC it's __attribute__((__packed__)). Consider this example on a 64-bit machine: struct foo { char *p; /* 8 bytes */ char c; /* 1 byte */ long x; /* 8 bytes */ ...
Suppose this struct is defined and compiled with a 32 bit compiler: struct test_32 { int a; // 4 byte short b; // 2 byte int c; // 4 byte } str_32; We might expect this struct to occupy only 10 bytes of memory, but by printing sizeof(str_32) we see it uses 12 by...

Page 1 of 1