[C++] 问个关于内存对齐的问题

为啥

struct FixedLengthHeader { uint32_t HeaderSize = 0; uint64_t CryptogramSize = 0; uint64_t ReservedField = 0;
}FixedPackageHeaders;

占用 20 字节( 4+8+8 ),但是如果用下面这个写法,

 struct FixedLengthHeader { uint32_t HeaderSize = 0; uint64_t CryptogramSize = 0; uint8_t DevFlag = 0; uint8_t HeaderVer = 0; uint32_t PackagerVer = 0; uint16_t Reserved = 0 }FixedPackageHeaders;

会因为内存对齐占用 24 字节(4+8+2+2+4+4)的内存呢……

发表评论

您的电子邮箱地址不会被公开。