Extra bytes when declaring a member of a struct as uint32_t

后端 未结 4 1842
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 10:32

I have a problem when using the uint32_t type from the stdint.h library. If I run the following code (on Ubuntu linux 11.10 x86_64, g++ version 4.6.1):

#include          


        
4条回答
  •  情歌与酒
    2021-01-21 10:39

    It's because of alignment. On your platform uint32_t needs to be 4 byte aligned. In order to achieve that dest_addr_64_h has to have two bytes of padding right in front of it because the position right after the two uint8_t members is a multiple of 2 but not 4.

    You can use the macro offsetof() to figure out exactly where members are placed within a struct, to see that this is true.

    You can either try to make the compiler pack the members tighter together, or you can rearrange the members so that padding isn't needed.

提交回复
热议问题