为什么不GCC优化结构? [英] Why doesn't GCC optimize structs?

查看:103
本文介绍了为什么不GCC优化结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

系统的需求,某些原语被对准到存储器内的某些点(整型到是4的倍数,短裤是2的倍数字节等等字节)。当然,这些可以被优化浪费在填充所述至少的空间。

Systems demand that certain primitives be aligned to certain points within the memory (ints to bytes that are multiples of 4, shorts to bytes that are multiples of 2, etc.). Of course, these can be optimized to waste the least space in padding.

我的问题是,为什么不GCC自动做到这一点?是比较明显的启发式(为了从变量最大尺寸要求到最小)缺乏某种方式?一些code依赖于它的结构的物理顺序(是个好主意)?

My question is why doesn't GCC do this automatically? Is the more obvious heuristic (order variables from biggest size requirement to smallest) lacking in some way? Is some code dependent on the physical ordering of its structs (is that a good idea)?

我只问,因为GCC在很多方面优化超强,但不是在这一个,我想肯定会有一些比较酷的解释(对此我忘记)。

I'm only asking because GCC is super optimized in a lot of ways but not in this one, and I'm thinking there must be some relatively cool explanation (to which I am oblivious).

推荐答案

GCC不重新排列结构的元素,因为这将违反C标准。 C99标准状态的部分6.7.2.1:

gcc does not reorder the elements of a struct, because that would violate the C standard. Section 6.7.2.1 of the C99 standard states:

在一个结构物中,非比特场部件和单元,其中比特音响视场
  居住有在其声明的顺序增加地址。

Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared.

这篇关于为什么不GCC优化结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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