为什么结构存储的字节数超过了其定义?

问题描述

输入以下代码:

#include <iostream>

using namespace std;

int main() {
    struct people {char name[50]; int tt; int gg;};
    
    struct people p1;
    cout << sizeof(p1);
    
    return 0;
}

输出为60。为什么?struct仅需要50 + 4 + 4 = 58个字节。如果将name[50]更改为name[20],我们将得到输出:28如预期。

与结构成员使用的内存是连续的并不矛盾?。

解决方法

为了提高效率(主要是执行速度),编译器会将Start-Process $dest变量对准4字节边界。因此,您的结构的布局为:

int

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...