C - 结构的第 16 个字节上的 16 位 int 跳过一个字节



在长度超过 16 字节的c 结构中,如果它是 2 字节的元素,则第 16 个字节就会出现问题。让我用代码解释一下:

struct test{
unsigned int a : 16;
unsigned int b : 16;
unsigned int c : 16;
unsigned int d : 16;
unsigned int e : 16;
unsigned int f : 16;
unsigned int g : 16;
unsigned int h : 8;
unsigned int i : 16;
};
int main(int argc, char *argv[]){
//bytes 0, 1, 16, 17 are all equal to 0x31 
unsigned char *data = "1134567890123451189";
struct test *testStructure = (struct test*) data;
printf("%X %Xn", testStructure->a, testStructure->i);
return 0;
}

输出:

3131 3831

为什么"i"不等于"a"?"i"跳过了字节 16,改用了字节 17 和 18。这是怎么回事?

我不会这样做,因为:

  1. 位字段打包是实现定义的。参见 C99 §6.7.2.1p10:">单元内位字段的分配顺序(高阶到低阶或低阶到高阶)是实现定义的">
  2. 这违反了严格的别名规则。

在这种情况下,实际发生的情况最有可能i4 字节(大小为int)对齐。

您可以禁用对齐,它应该会给您更好的结果:

#pragma pack(1)
struct test{
unsigned int a : 16;
unsigned int b : 16;
unsigned int c : 16;
unsigned int d : 16;
unsigned int e : 16;
unsigned int f : 16;
unsigned int g : 16;
unsigned int h : 8;
unsigned int i : 16;
};
#pragma pack()
int main(int argc, char *argv[]){
//bytes 0, 1, 15, 16 are all equal to 0x31 
unsigned char *data = "1134567890123451189";
struct test *testStructure = (struct test*) data;
printf("%X %Xn", testStructure->a, testStructure->i);
return 0;
}

在叮当声中,它打印x86_64:

3131 3131

但是,此代码仍然是非法的,不能保证在任何地方都以这种方式工作。

要解决位域问题,请尽量不要使用位域(幸运的是,在您的特定情况下,这是可能的)。但不幸的是,混叠问题没有简单的解决方案;大多数依赖类型双关语的人只是用-fno-strict-aliasing编译(包括Linux内核的人)。其他人使用unions 跳过箍,严格来说这仍然是非法的,但这是常见的成语,并且得到了大多数编译器的良好支持:

#include <stdio.h>
#include <stdint.h>
#pragma pack(1)
struct test{
uint16_t a;
uint16_t b;
uint16_t c;
uint16_t d;
uint16_t e;
uint16_t f;
uint16_t g;
uint8_t  h;
uint16_t i;
};
union u{
struct test t;
char str[17];
};
#pragma pack()
int main(int argc, char *argv[]){
//bytes 0, 1, 15, 16 are all equal to 0x31 
char *data = "1134567890123451189";
union u *testStructure = (union u*) data;
printf("%X %Xn", testStructure->t.a, testStructure->t.i);
return 0;
}

相关内容

最新更新