简体   繁体   English

为什么GCC不优化结构?

[英]Why doesn't GCC optimize structs?

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.). 系统要求某些基元与存储器内的某些点对齐(对于4的倍数的字节,对于2的倍数的字节的短路,等等)。 Of course, these can be optimized to waste the least space in padding. 当然,这些可以被优化以浪费填充中的最小空间。

My question is why doesn't GCC do this automatically? 我的问题是GCC为什么不自动执行此操作? 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)? 一些代码是否依赖于其结构的物理排序(这是一个好主意)?

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在很多方面都是超级优化的,但不是在这个方面,我认为必须有一些相对很酷的解释(我不知道)。

gcc does not reorder the elements of a struct, because that would violate the C standard. gcc不会重新排序结构的元素,因为这会违反C标准。 Section 6.7.2.1 of the C99 standard states: C99标准第6.7.2.1节规定:

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. 在结构对象中,非位字段成员和位字段所在的单元具有按声明顺序增加的地址。

Structs are frequently used as representations of the packing order of binary file formats and network protocols. 结构经常用作二进制文件格式和网络协议的打包顺序的表示。 This would break if that were done. 如果这样做会破坏。 In addition, different compilers would optimize things differently and linking code together from both would be impossible. 此外,不同的编译器会以不同的方式优化事物,并且将代码从两者链接在一起是不可能的。 This simply isn't feasible. 这根本不可行。

GCC is smarter than most of us in producing machine code from our source code; GCC比我们大多数人从我们的源代码生产机器代码更聪明; however, I shiver if it was smarter than us in re-arranging our structs, since it's data that eg can be written to a file. 然而,如果它比我们更聪明地重新安排我们的结构,我会颤抖,因为它的数据例如可以写入文件。 A struct that starts with 4 chars and then has a 4 byte integer would be useless if read on another system where GCC decided that it should re-arrange the struct members. 如果在另一个GCC决定它应该重新安排结构成员的系统上读取,那么以4个字符开头然后具有4字节整数的结构将是无用的。

gcc SVN确实有一个结构重组优化(-fipa-struct-reorg),但它需要整个程序分析,目前还不是很强大。

C compilers don't automatically pack structs precisely because of alignment issues like you mention. 由于您提到的对齐问题,C编译器不会自动打包结构。 Accesses not on word boundaries (32-bit on most CPUs) carry heavy penalty on x86 and cause fatal traps on RISC architectures. 不在字边界上访问(大多数CPU上为32位)会对x86造成严重损失,并在RISC体系结构上造成致命陷阱。

Not saying it's a good idea, but you can certainly write code that relies on the order of the members of a struct. 不是说这是一个好主意,但你当然可以编写依赖于结构成员顺序的代码。 For example, as a hack, often people cast a pointer to a struct as the type of a certain field inside that they want access to, then use pointer arithmetic to get there. 例如,作为一个黑客,人们经常会将一个指向结构的指针作为他们想要访问的某个字段的类型,然后使用指针算法来实现。 To me this is a pretty dangerous idea, but I've seen it used, especially in C++ to force a variable that's been declared private to be publicly accessible when it's in a class from a 3rd party library and isn't publicly encapsulated. 对我来说,这是一个非常危险的想法,但我已经看到它被使用,特别是在C ++中强制一个被声明为私有的变量,当它在第三方库的类中时是公开可访问的,并且不是公开封装的。 Reordering the members would totally break that. 重新排序成员将彻底打破这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM