了解堆栈分配和调整 [英] Understanding stack allocation and alignment

查看:160
本文介绍了了解堆栈分配和调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何对齐作品堆在什么是栈对齐<? / A>,但我有麻烦了一个小例子来演示该行为。我检查我的函数foo的堆栈分配:

I'm trying to understand how stack alignment works as described in what is "stack alignment"? but I have trouble getting a small example to demonstrate the said behaviour. I'm examining the stack allocation of my function foo:

void foo() {
    int a = 0;
    char b[16];
    b[0] = 'a';
}

我编译源文件, gcc的-ggdb example.c -o example.out (即没有任何编译器标志)和GDB汇编转储写着:

I compiled the source file with gcc -ggdb example.c -o example.out (i.e without any compiler flags) and the assembler dump from gdb reads:

(gdb) disassemble foo
Dump of assembler code for function foo:
0x08048394 <+0>:    push   %ebp
0x08048395 <+1>:    mov    %esp,%ebp
0x08048397 <+3>:    sub    $0x20,%esp
0x0804839a <+6>:    movl   $0x0,-0x4(%ebp)
0x080483a1 <+13>:   movb   $0x61,-0x14(%ebp)
0x080483a5 <+17>:   leave  
0x080483a6 <+18>:   ret    
End of assembler dump.

我的堆栈中的16个字节(我验证了这一点与其他几个测试)块分配。根据汇编转储这里32个字节被分配,因为(16 4; + 16&下; 32),但我预期整数'a'到在第一个16字节被分配,然后该字符阵列上的下一个将被分配16个字节(离开的在中间的12个字节的空间)。但似乎这两个整数和字符数组已分配的20个字节的连续的大块,这是低效按照我上面提到的讨论。是否有人可以解释我在这里丢失?

My stack is allocated in chunks of 16 bytes (I verified this with several other tests). According to the assembler dump here 32 bytes have been allocated because (16 < 4+16 < 32), however I expected integer 'a' to be allocated on the first 16 bytes and then the character array to be allocated on the next 16 bytes (leaving a space of 12 bytes in-between). But it seems both the integer and the character array have been allocated a contiguous chunk of 20 bytes, which is inefficient as per the discussion i referred above. Can someone please explain what I'm missing here?

修改:我来,我的堆栈中的16字节的块与程序如下图所示分配的结论是:

EDIT: I came to the conclusion that my stack is allocated in chunks of 16 bytes with a program like below:

void foo() {
    char a[1];
}

和相应的汇编转储:

(gdb) disassemble foo
Dump of assembler code for function foo:
0x08048394 <+0>:    push   %ebp
0x08048395 <+1>:    mov    %esp,%ebp
0x08048397 <+3>:    sub    $0x10,%esp
0x0804839a <+6>:    leave  
0x0804839b <+7>:    ret    
End of assembler dump.

您可以看到,16字节都被分配在堆栈尺寸为1的字符数组(仅1个字节需要)。我可以增加阵列的尺寸多达16和汇编转储保持不变,但是当它为17,它在栈上分配32个字节。我已经运行许多这样的样品,其结果是一样的;堆栈存储器中的16字节的块分配。类似的话题在堆栈分配,填充和对齐但我' m时找出更多的热衷就是对准了在我的例子没有效果。

You can see that 16 bytes have been allocated on the stack for a character array of size 1 (only 1 byte needed). i can increase the size of the array up to 16 and the assembler dump stays the same, but when it is 17, it allocates 32 bytes on the stack. I have run many such samples and the result is the same; stack memory is allocated in chunks of 16 bytes. A similar topic has been discussed in Stack allocation, padding, and alignment but what I'm more keen on finding out is why alignment has no effect in my example.

推荐答案

我觉得你失踪的事实,没有要求所有的堆栈变量进行单独对齐16字节边界。

I think you're missing the fact that there is no requirement for all stack variables to be individually aligned to 16-byte boundaries.

这篇关于了解堆栈分配和调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆