why do we need to have #pragma pack
for typedef
structure in C++? Specifically when you are using those structure in network communication.

- 49,413
- 29
- 133
- 174

- 31
- 1
- 2
-
Here is all info you should need. It was 1-st in the google by search query `pragma pack`: http://msdn.microsoft.com/en-us/library/aa273913%28v=vs.60%29.aspx – bezmax Dec 22 '10 at 09:24
-
What does this have to do with object oriented programming? – Jörgen Sigvardsson Dec 22 '10 at 09:24
4 Answers
#pragma pack
controls the alignment of members of a structure. The common default setting is 8, ensuring that members that are up to 8 bytes long get aligned on an address that's a multiple of their size. A double or 64-bit pointer for example. Reading or writing a mis-aligned double can be quite expensive, typically three times slower if it straddles a CPU cache line boundary. This alignment can produce unused space between members, called padding.
This kind of alignment is often inappropriate for network frames, they tend to be tightly packed without any padding, #pragma pack(push, 1)

- 922,412
- 146
- 1,693
- 2,536
-
3+1: Another point is: If the struct is not packed and you want to build a (CRC) checksum it becomes difficult because you have to access every single member of the struct whereas with a packing of 1 you can simply access all bytes of the struct. – ur. Dec 22 '10 at 09:45
The #pragma pack directive modifies the current alignment rule for only the members of structures whose declarations follow the directive. It does not affect the alignment of the structure directly, but by affecting the alignment of the members of the structure, it may affect the alignment of the overall structure according to the alignment rule

- 31
- 1
- 2
If you just need to let 2 systems with same setup talk to each other (test,...), you can send structs, provided they are just POD (plain old data) (pointers, virtual tables, std::strings... can't be used). So pack is not needed.
If the systems don't have the same setup (or are not known), you need to send data serialized in a protocol, so packed structures will not do. It is easiest to check whether valid protcols are available.

- 14,072
- 2
- 31
- 53
Compilers will "pad" the struct's fields and subfields, i.e., organize them in memory with blank chunks of memory that hold nothing in the middle. It does this to increase efficiency as Hans Passant's answer explains.
Different compilers/targets/optimization settings may pad differently, transforming equal 'logical' structs in unequal memory representations, so if two communicating machines pass the struct between them, they may not understand each other.
#pragma pack is your way to limit the compiler's freedom to pad.
#pragma pack(push, 1) orders the compiler to not pad at all. If you do this to two identical structs defined in different codes, they will be identically represented in memory. Then you can safely send the contents of a struct in one application throught some protocol using a pointer and sizeof(), getting it on the other side and writing it right over an identical struct, also using a pointer and sizeof().

- 2,532
- 3
- 26
- 30