一、做了啥?
// 定义了一个结构体
typedef struct _A
{
UINT32 a1;
UINT32 a2;
} A;
// 声明了一个结构体变量
A a_temp;
// 初始化a_temp
for (i = 0; i < sizeof(A)/sizeof(UINT32); i++)
{
*(&(a_temp.a1)+ i) = 0;
}
2.出现了什么现象
用o2编译,没有任何问题。
用o3编译,报-Warray-bounds 警告,警告内容如下:
cc1.exe: warning: '__builtin_memset' offset [x, x] from the object at 'A' is out of the bounds of referenced subobject 'a1' with type 'long unsigned int' at offset 0 [-Warray-bounds]
3.原因
应该是编译器的bug,https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92879
O3优化时,for循环初始化,直接用memset代替了,导致出现bug。但具体啥原因,不清楚编译器咋造成的。