why do we need to have #pragma pack for typedef structure in C++? Specifically when you are using those structure in network communication.
#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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With