#pragma pack in C ++

why do we have a structure #pragma packfor typedefin c ++? In particular, when you use this structure in network communication.

+3
source share
4 answers

#pragma packcontrols the alignment of structural elements. The usual default value is 8, ensuring that members with a length of up to 8 bytes are aligned at an address that is a multiple of their size. For example, a double or 64-bit pointer. Reading or writing a bidirectional double can be quite expensive, usually three times slower if it crosses the line boundary of the CPU cache. This alignment can create unused space between members, called padding.

, , , - , #pragma pack (push, 1)

+13

#pragma pack , . , , , .

0

(test,...), , POD ( ) (, , std:: ... ). .

( ), , , . , protcols.

0

"" , .. , . , Hans Passant.

// , "" , , , .

#pragma pack - .

#pragma pack (push, 1) . , , . , sizeof(), , sizeof().

0

Source: https://habr.com/ru/post/1781621/


All Articles