I am experiencing a very strange issue using gcc-4.7 (Ubuntu/Linaro 4.7.2-11precise2) 4.7.2. I am unable to compile the following valid code without a warning:
extern void dostuff(void);
int test(int arg1, int arg2)
{
int ret;
if (arg1) ret = arg2 ? 1 : 2;
dostuff();
if (arg1) return ret;
return 0;
}
Compile options and output:
$ gcc-4.7 -o test.o -c -Os test.c -Wall
test.c: In function ‘test’:
test.c:5:6: warning: ‘ret’ may be used uninitialized in this function [-Wmaybe-uninitialized]
However, the following code compiles with no warning (albeit to slightly less efficient assembly):
extern void dostuff(void);
int test(int arg1, int arg2)
{
int ret;
if (arg1 && arg2) ret = 1;
if (arg1 && !arg2) ret = 2;
dostuff();
if (arg1) return ret;
return 0;
}
I am somewhat stuck and am considering this a compiler bug. Any thoughts?
ret == arg2 ? 1 : 2;??arg1=0, arg2=0, return 1 ifarg1=1, arg2=1, return 2 ifarg1=1, arg2=0. This snippet is a simplified case of a much larger issue I'm having.volatilealso resolves the issue, but isn't really ideal.retis a large array which I don't want to initialize if it is not used. Looking at my first program, ret is indeed never used uninitialized, so the warning is incorrect, no?retor it will not initialize but then also never useretat all. Your objection is incorrect, the code as shown above will for sure never accessretbefore it is initialized, an explicit zero initializing is not needed, and if the compiler claims anything else, it's broken.