1

我有两个结构:

struct struct_A
{
  uint8_t v1[4];   // 4 bytes
  uint8_t v2;
};

struct struct_B
{
  uint32_t v1;  // 4 bytes
  uint8_t v2; // 1 byte
};

我通过使用代码获得它们的大小:

std::cout<<"size of struct_A is: "<<(sizeof(struct_A))    <<std::endl;
std::cout<<"size of struct_A::v1 is: "<<(sizeof(struct_A::v1))<<std::endl;
std::cout<<"size of struct_A::v2 is: "<<(sizeof(struct_A::v2))<<std::endl;
std::cout<<"size of struct_B is: "<<(sizeof(struct_B))    <<std::endl;
std::cout<<"size of struct_B::v1 is: "<<(sizeof(struct_B::v1))<<std::endl;
std::cout<<"size of struct_B::v2 is: "<<(sizeof(struct_B::v2))<<std::endl;

我得到输出:

size of struct_A is: 5
size of struct_A::v1 is: 4
size of struct_A::v2 is: 1
size of struct_B is: 8
size of struct_B::v1 is: 4
size of struct_B::v2 is: 1

我如何理解 struct_A 的大小和 struct_B 的大小不同?谢谢。

4

0 回答 0