GCC 以相反的顺序编译 EEPROM 地址

GCC compiles EEPROM addresses in reverse order

我有一个用 C 编写并用 GCC 编译器编译的 ATmega 代码。

一些数据需要存储在 EEPROM 中,所以我添加了这些声明:

读取EEPROM后,我发现数据以某种奇怪的方式放置。经过一番调查后,我在 .map 文件(工具链生成的许多文件之一)中找到了这段文字:

如您所见,编译器将数据倒序排列。

当然我可以反向声明并享受进一步的编码,但这是意想不到的事情,所以我害怕面对任何其他意想不到的行为,直到我不明白为什么编译器会这样做。

有什么想法吗?

如@Lundin 所述,全局变量不需要连续分配。

您可以使用 struct 的特性,它的成员总是被分配 in the same order 它们是指定的。
来自 C11 标准,§6.7.2.1.15:

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.

注意padding,不过!