Originally Posted by
edadasiewicz
The alignment of items within structures, classes, and unions is not defined, except that members are laid out in order of declaration. This is a direct quote from The Practice of Programming by Kernighan and Pike. The reason is that different machine architectures require different data types (char, int, float, ...) to be aligned on certain memory boundaries.
The default alignment of structure members can be changed. This may save space but the members are then less efficient to access.
Code:
#include <stdio.h>
struct s_t {
int a;
char b;
int c;
};
#pragma pack(1)
struct t_t {
int a;
char b;
int c;
};
int main()
{
printf("%d\n", sizeof(struct s_t));
printf("%d\n", sizeof(struct t_t));
return 0;
}
Prints 12 and 9 for me.
Bookmarks