pragma pack
-
#pragma pack(1) struct X { int a; char b; int c; }; int main() { struct X x; printf("%d", sizeof(x)); } mit pragma pack(1): 9 ohne pragma pack(1): 12 in der doku zu #pragma pack(1) steht etwas von byte-aligned...was ist das? wie kommt man auf 12 bzw 9 byte?
-
So sieht das im Speicher aus.
Mit alignment:
aaaabpppcccc
Ohne alignment:
aaaabcccc
Wobei a, b und c die Variablennamen sind und p padding bytes ("Füllbytes").
Vorteil von alignment. Der Zugriff auf die Variable c ist schneller.
Nachteil sollte klar sein. Speicher wird verschwendet.
-
9Byte: 4 Byte int, 1 Byte char, 4 Byte int
12Byte: 4 Byte int, 1 Byte char, 3 Byte leer, 4 Byte int
-
warum spricht man von Vier-Byte-Alignment bei einer luecke in einer struct von drei bytes?
-