Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 是否可以在单字节对齐结构中对齐特定结构成员?_C_Linux_Alignment_Arm_Structure - Fatal编程技术网

C 是否可以在单字节对齐结构中对齐特定结构成员?

C 是否可以在单字节对齐结构中对齐特定结构成员?,c,linux,alignment,arm,structure,C,Linux,Alignment,Arm,Structure,我在用手臂。由于奇数偏移量的读/写(我们知道ARM是4字节对齐的),我得到了对齐错误。我的程序中定义的所有结构都是单字节对齐的,就像 #pragma pack(push, 1) typedef struct { char a1; int a2; char a3; }a; #pragma pack(pop) 我正在努力 #pragma pack(push, 1) typedef struct { char a1 __attribute__ ((align

我在用手臂。由于奇数偏移量的读/写(我们知道ARM是4字节对齐的),我得到了对齐错误。我的程序中定义的所有结构都是单字节对齐的,就像

#pragma pack(push, 1)    

typedef struct
{
   char a1;
   int  a2;
   char a3;
}a;

#pragma pack(pop)
我正在努力

#pragma pack(push, 1)

typedef struct
{
    char a1 __attribute__ ((aligned (4)));
    int  a2;
    char a3;
}a;

#pragma pack(pop) 
gcc属性\u属性((对齐(4))无效

Note :: The above code is not my actual code. sample scenario. 


所以我重新安排了结构成员以解决对齐问题。我想确定重新安排是否是可能的解决方案,或者我们可以使\u属性\u处理此场景。欢迎任何其他解决方案。提前谢谢

您可以在ARM上安全地读/写字节对齐结构中的char/int,编译器会注意对齐。对齐时可能出现的一个问题是转换为32位int,如下所示:

char buf[5];
char *p = buf + 1;
*((int *)p) = 1;
注意:如果出于某些原因,例如对齐某个成员(从结构的开头),可以使用以下技巧:

typedef struct {
    struct {} __attribute__ ((aligned (4)));
    char a1; // offset is 4
    int  a2; // offset is 5
    char a3; // offset is 9
} a;

如果一个结构是使用#pragma单字节对齐的,那么是否可以将该结构成员对齐到4字节?您可以:尝试这样做:int uuu属性uuu((对齐(4)))a2;尝试结构大小没有变化。只给出6。注意:我想对齐a1(字符),而不是a2。我喜欢这种类型的问题。似乎#pragma覆盖了#attibute。是否可以删除#pragma?并在必要时使用u属性u((打包))?我无法删除#pragma。我正在读取这样的USB数据。这些结构应该是单字节压缩的。好的,那么您可以只为这些USB结构设置((压缩))属性,并删除pragma。结构的第一个成员从偏移量0开始,因此它必须与结构本身具有相同的对齐方式。所以我不能在#pragma???中使用属性???