Strict warning about aliasing on gcc 4.6.1 bug

I am trying to compile the following in gcc with -pedantic-errors -pedantic -Wall -O2

 #include <iostream> void reset_uint32(uint32_t* pi) { char* c = (char*)(pi); uint16_t* j = (uint16_t*)(c); // warning? j[0] = 0; j[1] = 0; } void foo() { uint32_t i = 1234; reset_uint32(&i); } int main() { foo(); } 

But I do not see strict alias warnings. I also tried turning on

 -fstrict-aliasing -Wstrict-aliasing 

but still no warnings. This is mistake?

+6
source share
1 answer

I rewrote your example to issue a warning about breaking anti-aliasing rules:

 void foo(int* pi) { short* j = (short*)pi; j[0] = j[1] = 0; } int main() { int i = 1234; foo(&i); short* j = (short*)&i; j[0] = j[1] = 0; } 

Although g ++ 4.6 only shows a warning, if you compile code with -Wstrict-aliasing=2 instead of -Wstrict-aliasing . In addition, it only shows a warning for translation in main() , and not in foo() . But I do not see how / why the compiler will look at these two roles differently.

+1
source

Source: https://habr.com/ru/post/904031/


All Articles