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

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

问题描述

系统要求某些原语与内存中的某些点对齐(int 到 4 的倍数的字节,shorts 到 2 的倍数的字节等).当然,可以对这些进行优化,以在 padding 中浪费最少的空间.

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 不自动执行此操作?是否缺少某种更明显的启发式(从最大尺寸要求到最小尺寸的顺序变量)?某些代码是否依赖于其结构的物理顺序(这是个好主意)吗?

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天全站免登陆