了解 C++结构体大小

发布于 2024-10-11 00:03:00 字数 881 浏览 8 评论 0原文

考虑以下代码:

struct CExample  {
    int a; 
}            

int main(int argc, char* argv[]) {  

    CExample ce1;  
    CExample ce2;  

    cout << "Size:" << sizeof(ce1) << " Address: " << &ce1 << endl;   
    cout << "Size:" << sizeof(ce2) << " Address: " << &ce2 << endl;   

    CExample ceArr[2];
    cout << "Size:" << sizeof(ceArr[0])<< " Address: "<< &ceArr[0] <<endl;
    cout << "Size:" << sizeof(ceArr[1])<< " Address: "<< &ceArr[1] <<endl;

    return 0;
}

输出示例:
ce1:大小=4,地址:0039FAA0
ce2:大小=4,地址:0039FA94
ceArr[0]:大小=4,地址:0039FA84
ceArr[1]: Size=4, Address: 0039FA88

在代码中,前两个对象(ce1 和 ce2)的地址之间存在 12 字节的差异,但数组中的对象之间仅存在 4 字节的差异。

我认为数据对齐与该问题有关,但我仍然感到困惑。知道这里到底发生了什么吗?

Consider the following code:

struct CExample  {
    int a; 
}            

int main(int argc, char* argv[]) {  

    CExample ce1;  
    CExample ce2;  

    cout << "Size:" << sizeof(ce1) << " Address: " << &ce1 << endl;   
    cout << "Size:" << sizeof(ce2) << " Address: " << &ce2 << endl;   

    CExample ceArr[2];
    cout << "Size:" << sizeof(ceArr[0])<< " Address: "<< &ceArr[0] <<endl;
    cout << "Size:" << sizeof(ceArr[1])<< " Address: "<< &ceArr[1] <<endl;

    return 0;
}

Output example:
ce1: Size=4, Address: 0039FAA0
ce2: Size=4, Address: 0039FA94
ceArr[0]: Size=4, Address: 0039FA84
ceArr[1]: Size=4, Address: 0039FA88

With the code there is a 12-byte between the addresses of the first two objects (ce1 and ce2) but there is only a 4-byte difference between the objects in the array.

I thought data-alignment would have something to do with the issue, but I'm still stumped. Any idea what is actually going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

亚希 2024-10-18 00:03:00

因为数组中的对象要求是连续的。在堆栈上连续声明的对象(在源代码中,而不是机器中)不[要求是连续的],尽管它们可以是。

Because objects in an array are required to be contiguous. Objects declared consecutively on the stack(in source code, not machine) are not[required to be contiguous], although they can be.

许一世地老天荒 2024-10-18 00:03:00

标准对此只字未提。编译器可以自由地在项之间插入任何它想要的填充。

(如果我不得不猜测,我猜测您的编译器在调试模式下实现了某种形式的堆栈保护/金丝雀(并且您正在调试模式下进行编译))

The standard says nothing about this. The compiler is free to insert whatever padding it wants between the items.

(If I had to guess, I'd guess that your compiler implements some form of stack protection/canary in debug mode (and that you are compiling in debug mode))

下雨或天晴 2024-10-18 00:03:00

编译器不仅使用堆栈来保存局部变量 - 它还使用它来传递参数,以及由 std::cout 引起的一些开销。这可能就是变量之间的额外空间的用途。

如果您将变量设置为静态,如下所示:

static CExample ce;
static CExample ce2;

static CExample ceArr[2];

...变量将被放置在 BSS 内存中,并且对齐方式更有可能符合您的预期。

其他答案阐明了数组被打包而单个项目未被打包的原因......

The compiler does not only use the stack for keeping your local variables - it also uses it for example for argument passing, and some of the overhead caused by std::cout. That's probably what the extra space between your variables is used for.

If you instead make your variables static, like this:

static CExample ce;
static CExample ce2;

static CExample ceArr[2];

...the variables will be placed in BSS memory instead, and the alignment will more likely be what you expect.

The reason why arrays are packed, and individual items are not, is clarified by other answers...

酒解孤独 2024-10-18 00:03:00

原因是,在大多数架构上,未对齐加载速度很慢。请参阅关于数据结构对齐的维基百科条目以获得详细解释。编译器将每个数据结构放置在数据字的开头。但是,在数组中,项目连续放置在内存中(按照 C++ 标准的要求)。

The reason is that, on most architectures, unaligned loads are slow. See the Wikipedia entry on data structure alignment for a good explanation. The compiler places each one of your data structures at the start of a data word. However, in arrays, the items are placed contiguously in memory (as the C++ standard requires).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文