c - 是否有 GCC 关键字允许结构重新排序?

标签 c gcc padding c99 memory-alignment

我知道为什么默认情况下 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 SVNgithub mirror 上尝试 struct-reorg-branch,风险自负,因为它仍在积极开发中。

您还可以使用 clang-reorder-fields 中的 clang-tools-extra 工具对字段重新排序

另见

关于c - 是否有 GCC 关键字允许结构重新排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14671253/

相关文章:

python-3.x - 如何在 Fedora 上安装旧版本的 gcc

c - 如何使用带有 Linux 子系统的 GCC 为 Windows 编译可执行文件?

css - 如何摆脱图像下方和上方的空白区域

javascript - 边框和内边距宽度 javascript

c - 为什么我们不能在一开始就定义指向 null 的结构指针

c - 如何将数组分解为固定大小的小数组? (在C中)

c - 如何从 malloc 区域执行一段代码

c - 使用 Gtk3,如何在辅助线程中等待用户输入?

gcc - 遵循 build-webos 的说明时,__libc_csu_fini 和 __libc_csu_init 未定义

c - C/GCC 4.6.3 中的 union 填充值