注: 此测试用了三种编译方式,结果均一致
1 - gcc test.c
2 - gcc test.c -O0
3 - cl test.c
问题
Test 1
#include <stdio.h>
union bits
{
struct __bits
{
unsigned char i0 : 1;
unsigned char i1 : 1;
unsigned char i2 : 1;
unsigned char i3 : 1;
unsigned char i4 : 1;
unsigned char i5 : 1;
unsigned char i6 : 1;
unsigned char i7 : 1;
};
unsigned char var;
};
int main()
{
union bits a;
unsigned char t;
printf("size = %d\n",sizeof(union bits));
scanf("%d", &t);
a.i0 = t;
printf("%d\n", a.var);
scanf("%d", &t);
a.i1 = t;
printf("%d\n", a.var);
scanf("%d", &t);
a.i2 = t;
printf("%d\n", a.var);
scanf("%d", &t);
a.i3 = t;
printf("%d\n", a.var);
scanf("%d", &t);
a.i4 = t;
printf("%d\n", a.var);
scanf("%d", &t);
a.i5 = t;
printf("%d\n", a.var);
scanf("%d", &t);
a.i6 = t;
printf("%d\n", a.var);
scanf("%d", &t);
a.i7 = t;
printf("%d\n", a.var);
return 0;
}
期望结果应是自由操纵每一位,而不改变其他位
实际结果是只能改变一位
Test 2
#include <stdio.h>
union bits
{
struct __bits
{
unsigned char i0 : 1;
unsigned char i1 : 1;
unsigned char i2 : 1;
unsigned char i3 : 1;
unsigned char i4 : 1;
unsigned char i5 : 1;
unsigned char i6 : 1;
unsigned char i7 : 1;
};
unsigned char var;
};
int main()
{
union bits a;
unsigned char t;
printf("size = %d\n",sizeof(union bits));
a.i0 = 1;
a.i1 = 1;
printf("%d\n", a.var);
a.i0 = 1;
a.i1 = 1;
scanf("%d", &t);
printf("%d\n", a.var);
return 0;
}
当我进行如上测试时,发现执行 scanf 会改变a的值
解决方案
思路1 - 添加 volatile 声明符
#include <stdio.h>
union bits
{
struct __bits
{
unsigned char i0 : 1;
unsigned char i1 : 1;
unsigned char i2 : 1;
unsigned char i3 : 1;
unsigned char i4 : 1;
unsigned char i5 : 1;
unsigned char i6 : 1;
unsigned char i7 : 1;
};
unsigned char var;
};
int main()
{
volatile union bits a;
unsigned char t;
printf("size = %d\n",sizeof(union bits));
a.i0 = 1;
a.i1 = 1;
printf("%d\n", a.var);
a.i0 = 1;
a.i1 = 1;
scanf("%d", &t);
printf("%d\n", a.var);
return 0;
}
结果还是老样子
思路2 - 添加 static volatile 声明符
我无意间想到是不是可以加个static试一下
#include <stdio.h>
union bits
{
struct __bits
{
unsigned char i0 : 1;
unsigned char i1 : 1;
unsigned char i2 : 1;
unsigned char i3 : 1;
unsigned char i4 : 1;
unsigned char i5 : 1;
unsigned char i6 : 1;
unsigned char i7 : 1;
};
unsigned char var;
};
int main()
{
static volatile union bits a;
unsigned char t;
printf("size = %d\n",sizeof(union bits));
a.i0 = 1;
a.i1 = 1;
printf("%d\n", a.var);
a.i0 = 1;
a.i1 = 1;
scanf("%d", &t);
printf("%d\n", a.var);
return 0;
}
结果发现,真可以,得到预期的结果了
把最初的程序加上 static volatile ,成功了
结论
加上 volatile 还可以理解, 但是为什么加上 static 就好了呢?
有大佬知道的,赐教一下