Disable structure padding in C without using pragma

前端 未结 6 652
遥遥无期
遥遥无期 2021-01-11 16:33

How can I disable structure padding in C without using pragma?

6条回答
  •  日久生厌
    2021-01-11 16:39

    Other than compiler options like pragma pack, you cannot, padding is in the C Standard.

    You can always attempt to reduce padding by declaring the smallest types last in the structure as in:

    struct _foo {
         int a;  /* No padding between a & b */
         short b;
    } foo;
    
    struct _bar {
         short b; /* 2 bytes of padding between a & b */
         int a;
    } bar;
    

    Note for implementations which have 4 byte boundaries

提交回复
热议问题