<分区>
我有一个类似的结构
struct Student
{
bool isActive;
};
现在如果我看到这个结构的大小输出如下:
Student student;
cout << sizeof(student);
然后它显示 1,这很好,因为 bool 的大小是 1 个字节。对于
struct Student
{
string name;
};
输出是 4,这又很好,因为字符串的大小是 4 个字节。 但对于以下内容:
struct Student
{
string name;
bool isActive;
};
我对 sizeof(student)
的期望是 4 + 1 = 5,但输出显示 8。为什么会这样?