Extra bytes when declaring a member of a struct as uint32_t

后端 未结 4 1840
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  -上瘾入骨i
    2021-01-21 10:55

    Structs are padded to be an integer multiple of 4 bytes1 so that they are word-aligned. http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding

    See also:

    • Why isn't sizeof for a struct equal to the sum of sizeof of each member?
    • Structure padding and packing
    • Use of struct padding

    1 As @Mooing Duck commented, this isn't always true:

    It's not always a multiple of 4 bytes, it varies (slightly) depending on the members. On the other hand, 99% of the time it's a multiple of 4 bytes.

提交回复
热议问题