Structure padding and packing

前端 未结 9 1218
太阳男子
太阳男子 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.

提交回复
热议问题