C编译器能重新堆栈变量? [英] Can a C compiler rearrange stack variables?

查看:106
本文介绍了C编译器能重新堆栈变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目工作中,我们已经修改了堆栈变量的声明的顺序,以减少生成的可执行文件的大小,在过去的嵌入式系统。例如,如果我们有:

I have worked on projects for embedded systems in the past where we have rearranged the order of declaration of stack variables to decrease the size of the resulting executable. For instance, if we had:

void func()
{
    char c;
    int i;
    short s;
    ...
}

我们将重新排序这是:

void func()
{
    int i;
    short s;
    char c;
    ...
}

由于对准问题第一个结果是正在使用12字节的堆栈空间而第二个结果是只有8个字节。

Because of alignment issues the first one resulted in 12 bytes of stack space being used and the second one resulted in only 8 bytes.

是C编译器这一标准的行为或只是我们使用的编译器的缺点?

Is this standard behavior for C compilers or just a shortcoming of the compiler we were using?

在我看来,一个编译器应该能够重新安排堆栈变量,如果它想青睐小可执行文件的大小。有人建议,我认为标准C prevents的某些方面,但我一直没能找到一个有信誉的来源无论哪种方式。

It seems to me that a compiler should be able to reorder stack variables to favor smaller executable size if it wanted to. It has been suggested to me that some aspect of the C standard prevents this, but I haven't been able to find a reputable source either way.

作为奖励的问题,这是否也适用于C ++编译器?

As a bonus question, does this also apply to C++ compilers?

修改

如果答案是肯定的,C / C ++编译器可以重新排列堆栈变量,你可以给一个编译器,绝对做到这一点的例子吗?我希望看到编译器文档或类似的东西,这个备份起来。

If the answer is yes, C/C++ compilers can rearrange stack variables, can you give an example of a compiler that definitely does this? I'd like to see compiler documentation or something similar that backs this up.

再次编辑

谢谢大家的帮助。有关文档,我已经能够找到的最好的事情是论文的最佳栈在GCC (PDF)插槽分配,由纳文Sharma和桑吉夫·库马尔·古普塔,这是$ p $在2003年海湾合作委员会首脑会议程序psented。

Thanks everybody for your help. For documentation, the best thing I've been able to find is the paper Optimal Stack Slot Assignment in GCC(pdf), by Naveen Sharma and Sanjiv Kumar Gupta, which was presented at the GCC summit proceedings in 2003.

有关项目使用ADS编译器ARM开发这里了。正是出于这个编译器,订货声明就像我已经证明可以提高性能,以及堆栈大小,因为如何在ARM的架构拇指在当地的堆栈帧计算地址文档中提到。那编译器不会自动重新排列当地人利用这一优势。此处链接白皮书说,截至2003年海湾合作委员会也没有重新排列堆栈帧提高了ARM的处理器的Thumb引用的局部性,但它意味着你可以。

The project in question here was using the ADS compiler for ARM development. It is mentioned in the documentation for that compiler that ordering declarations like I've shown can improve performance, as well as stack size, because of how the ARM-Thumb architecture calculates addresses in the local stack frame. That compiler didn't automatically rearrange locals to take advantage of this. The paper linked here says that as of 2003 GCC also didn't rearrange the stack frame to improve locality of reference for ARM-Thumb processors, but it implies that you could.

我找不到任何明确表示,这是有史以来在GCC实现的,但我觉得这纸计数,以证明你是正确的。再次感谢。

I can't find anything that definitely says this was ever implemented in GCC, but I think this paper counts as proof that you're all correct. Thanks again.

推荐答案

由于没有什么标准,禁止,对于C或C ++编译器,yes,编译器可以做到这一点。

As there is nothing in the standard prohibiting that for C or C++ compilers, yes, the compiler can do that.

有集料(即结构),其中,所述相对顺序必须保持不同,但还是编译器可以插入填充字节来实现preferable对齐。

It is different for aggregates (i.e. structs), where the relative order must be maintained, but still the compiler may insert pad bytes to achieve preferable alignment.

IIRC新MSVC编译器使用的自由度他们对当地人的缓冲区溢出的战斗。

IIRC newer MSVC compilers use that freedom in their fight against buffer overflows of locals.

作为一个方面说明,在C ++中,破坏秩序必须声明的顺序相反,即使编译器重新排序的内存布局。

As a side note, in C++, the order of destruction must be reverse order of declaration, even if the compiler reorders the memory layout.

(我不能提出旁证,虽然,这是从内存中。)

(I can't quote chapter and verse, though, this is from memory.)

这篇关于C编译器能重新堆栈变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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