繁体   English   中英

32位处理器字的默认结构对齐方式

[英]Default structure alignment for 32 bit processor word

我正在使用为32位ARM处理器编译的结构。

typedef struct structure {
   short a;
   char b;
   double c;
   int d;
   char e;
}structure_t;

如果我什么都不使用, __attribute__ ((aligned (8)))__attribute__ ((aligned (4)))我在结构大小和元素偏移方面得到相同的结果。 总大小为24.所以我认为它始终与8对齐(偏移是针对a=0b=2c=8d=16e=20 )。

为什么8是编译器选择的默认对齐方式? 不应该是4因为是32字处理器吗?

在此先感谢您的配偶。

aligned属性仅指定最小对齐,而不是精确对齐。 来自gcc 文档

aligned属性只能增加对齐; 但你也可以通过指定打包来减少它。

在你的平台上,double的自然对齐是8,所以这就是使用的。

因此,要获得您想要的内容,您需要组合alignedpacked属性。 使用以下代码,c具有偏移量4(使用offsetof测试)。

typedef struct structure {
   short a;
   char b;
   __attribute__((aligned(4))) __attribute__((packed)) double c;
   int d;
   char e;
} structure_t;

暂无
暂无

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

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