Why does the compiler allocate more than needed in the stack?

和自甴很熟 提交于 2019-11-27 05:37:18
torek

Compilers may indeed reserve additional memory for themselves.

Gcc has a flag, -mpreferred-stack-boundary, to set the alignment it will maintain. According to the documentation, the default is 4, which should produce 16-byte alignment, which needed for SSE instructions.

As VermillionAzure noted in a comment, you should provide your gcc version and compile-time options (use gcc -v to show these).

Because you haven't enabled optimization.

Without optimization, the compiler makes no attempt to minimize the amount of space or time it needs for anything in the generated code -- it just generates code in the most straight-forward way possible.

Add -O2 (or even just -O1) or -Os if you want the compiler to produce decent code.

I need 24 bytes in total.

The compiler needs space for a return address and a base pointer. As you are in 64 bit mode, that's another 16 bytes. Total 40. Round that up to a 32-byte boundary and you get 64.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!