我知道为什么默认情况下 GCC 不对结构的成员重新排序,但我很少编写依赖于结构顺序的代码,所以有什么方法可以将我的结构标记为自动重新排序?
最佳答案
以前的 GCC 版本有 -fipa-struct-reorg
option 允许在 -fwhole-program
+ -combine
模式下重新排序
-fipa-struct-reorg
Perform structure reorganization optimization, that change C-like structures layout in order to better utilize spatial locality. This transformation is affective for programs containing arrays of structures. Available in two compilation modes: profile-based (enabled with
-fprofile-generate
) or static (which uses built-in heuristics). Require-fipa-type-escape
to provide the safety of this transformation. It works only in whole program mode, so it requires-fwhole-program
and-combine
to be enabled. Structures considered'cold'
by this transformation are not affected (see--param struct-reorg-cold-struct-ratio=value
).
由于 release note 中的以下原因,它从 GCC 4.8.x 开始被删除
The struct reorg and matrix reorg optimizations (command-line options
-fipa-struct-reorg
and-fipa-matrix-reorg
) have been removed. They did not always work correctly, nor did they work with link-time optimization (LTO), hence were only applicable to programs consisting of a single translation unit.
但是您仍然可以在 GCC SVN 或 github mirror 上尝试 struct-reorg-branch
,风险自负,因为它仍在积极开发中。
您还可以使用 clang-reorder-fields 中的 clang-tools-extra 工具对字段重新排序
另见
关于c - 是否有 GCC 关键字允许结构重新排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14671253/