tags:

views:

66

answers:

1
+2  Q: 

C++ #pragma pack

What does the following statement actually do and what are it's effects?

#pragma pack(push,8)
+5  A: 

It pushes the current pack setting onto a stack (so that you can restore it later via pop) and then sets the alignment for struct elements to 8 bytes. Anything which is not naturally aligned to an 8 byte boundary will have padding bytes inserted before it to maintain the required alignment.

Paul R
Just an addition for how to see the effects: Define a structure with some elements of different sizes (e.g. `char`, `short`, `long`, `double`). Then determine the size of the structure using `sizeof`. Place a pair of `#pragma pack(push, n)` and `#pragma pack(pop)` around the structure with different values for `n` (e.g. 1, 2, 4, 8). See how `sizeof`'s results change. As an option, check out the change in relative addressing of the structure's elements.
Flinsch