Structure padding and packing

前端 未结 9 1225
太阳男子
太阳男子 2020-11-21 05:04

Consider:

struct mystruct_A
{
   char a;
   int b;
   char c;
} x;

struct mystruct_B
{
   int b;
   char a;
} y;

The sizes of the structur

相关标签:
9条回答
  • 2020-11-21 05:11

    Padding aligns structure members to "natural" address boundaries - say, int members would have offsets, which are mod(4) == 0 on 32-bit platform. Padding is on by default. It inserts the following "gaps" into your first structure:

    struct mystruct_A {
        char a;
        char gap_0[3]; /* inserted by compiler: for alignment of b */
        int b;
        char c;
        char gap_1[3]; /* -"-: for alignment of the whole struct in an array */
    } x;
    

    Packing, on the other hand prevents compiler from doing padding - this has to be explicitly requested - under GCC it's __attribute__((__packed__)), so the following:

    struct __attribute__((__packed__)) mystruct_A {
        char a;
        int b;
        char c;
    };
    

    would produce structure of size 6 on a 32-bit architecture.

    A note though - unaligned memory access is slower on architectures that allow it (like x86 and amd64), and is explicitly prohibited on strict alignment architectures like SPARC.

    0 讨论(0)
  • 2020-11-21 05:11

    Structure packing suppresses structure padding, padding used when alignment matters most, packing used when space matters most.

    Some compilers provide #pragma to suppress padding or to make it packed to n number of bytes. Some provide keywords to do this. Generally pragma which is used for modifying structure padding will be in the below format (depends on compiler):

    #pragma pack(n)
    

    For example ARM provides the __packed keyword to suppress structure padding. Go through your compiler manual to learn more about this.

    So a packed structure is a structure without padding.

    Generally packed structures will be used

    • to save space

    • to format a data structure to transmit over network using some protocol (this is not a good practice of course because you need to
      deal with endianness)

    0 讨论(0)
  • 2020-11-21 05:14

    Structure packing is only done when you tell your compiler explicitly to pack the structure. Padding is what you're seeing. Your 32-bit system is padding each field to word alignment. If you had told your compiler to pack the structures, they'd be 6 and 5 bytes, respectively. Don't do that though. It's not portable and makes compilers generate much slower (and sometimes even buggy) code.

    0 讨论(0)
  • 2020-11-21 05:21

    I know this question is old and most answers here explains padding really well, but while trying to understand it myself I figured having a "visual" image of what is happening helped.

    The processor reads the memory in "chunks" of a definite size (word). Say the processor word is 8 bytes long. It will look at the memory as a big row of 8 bytes building blocks. Every time it needs to get some information from the memory, it will reach one of those blocks and get it.

    Variables Alignment

    As seem in the image above, doesn't matter where a Char (1 byte long) is, since it will be inside one of those blocks, requiring the CPU to process only 1 word.

    When we deal with data larger than one byte, like a 4 byte int or a 8 byte double, the way they are aligned in the memory makes a difference on how many words will have to be processed by the CPU. If 4-byte chunks are aligned in a way they always fit the inside of a block (memory address being a multiple of 4) only one word will have to be processed. Otherwise a chunk of 4-bytes could have part of itself on one block and part on another, requiring the processor to process 2 words to read this data.

    The same applies to a 8-byte double, except now it must be in a memory address multiple of 8 to guarantee it will always be inside a block.

    This considers a 8-byte word processor, but the concept applies to other sizes of words.

    The padding works by filling the gaps between those data to make sure they are aligned with those blocks, thus improving the performance while reading the memory.

    However, as stated on others answers, sometimes the space matters more then performance itself. Maybe you are processing lots of data on a computer that doesn't have much RAM (swap space could be used but it is MUCH slower). You could arrange the variables in the program until the least padding is done (as it was greatly exemplified in some other answers) but if that's not enough you could explicitly disable padding, which is what packing is.

    0 讨论(0)
  • 2020-11-21 05:24

    There are no buts about it! Who want to grasp the subject must do the following ones,

    • Peruse The Lost Art of Structure Packing written by Eric S. Raymond
    • Glance at Eric's code example
    • Last but not least, don't forget the following rule about padding that a struct is aligned to the largest type’s alignment requirements.
    0 讨论(0)
  • 2020-11-21 05:29

    Padding and packing are just two aspects of the same thing:

    • packing or alignment is the size to which each member is rounded off
    • padding is the extra space added to match the alignment

    In mystruct_A, assuming a default alignment of 4, each member is aligned on a multiple of 4 bytes. Since the size of char is 1, the padding for a and c is 4 - 1 = 3 bytes while no padding is required for int b which is already 4 bytes. It works the same way for mystruct_B.

    0 讨论(0)
提交回复
热议问题