I have see several knowledge article about this topic. The rules is not very complicated. But maybe there are several concepts should be clarified. If we understand them clearly, this topic is so easy.
1) alignment is processed in compile-time;
By the way, sizeof is also be processed in compile-time just like array length is determined at compile-time.
2) if you have assigned a value n to pragma pack directive, e.g. #pragma pack(n), the compiles will compare the value n with the minimal size of the struct and choose the less one.
e.g.
struct struct1{
char a;
int b;
double c;
char*p;
};
default sizeof(struct1) is 20;
for pack(1), the value is 17;
for pack(2), the value is 18;
for pack(4), the value is 20;
for pack(8), the value still is 20!
I have test it with GCC 4.1.2.
发表于 @ 2008年04月06日 11:18:00|评论(loading...)|编辑