9

So let's say I'm careless and make a stupid typo.. this file:

test.c

#include <stdio.h>
int main()
{
 int x = x;
 printf("%d\n",x);
}

compiles fine:

mymachine:~ oll$ gcc test.c 
mymachine:~ oll$ ./a.out 
1782198366

Obviously int x = x is a mistake but the compiler accepts this without warning. I've wasted quite a few hours trying to this error.

Is there a compiler flag that and can use for gcc / g++ to make the compiler give me a warning when I use an uninitialised stack variable? This could potentially save me a lot of time in the future.

I have tried gcc -O -Wuninitialized test.c - didn't work.

Thanks in advance

Edit: I have tried -Wall, no mention of x

mymachine:~ oll$ gcc -Wall test.c 
test.c: In function ‘main’:
test.c:7: warning: control reaches end of non-void function

Edit: Solution found

It seems that using the command line tools gcc and g++ in OS X 10.8 doesn't give this warning, using clang works:

mymachine:~ oll$ clang -Wall test.c
test.c:5:10: warning: variable 'x' is uninitialized when used within its own initialization [-Wuninitialized]
 int x = x;
 ~ ^
1 warning generated.
Shafik Yaghmour
160k44 gold badges466 silver badges778 bronze badges
asked Apr 9, 2014 at 13:02
6
  • 5
    You should always be using gcc -Wall ... Commented Apr 9, 2014 at 13:04
  • asked something similar for Visual Studio, still waiting for an answer Commented Apr 9, 2014 at 13:04
  • You can set the compilation as stricter. See related info here: stackoverflow.com/questions/490737/… Commented Apr 9, 2014 at 13:04
  • -Wall gives me a test.c:7: warning: control reaches end of non-void function, no mention of x Commented Apr 9, 2014 at 13:05
  • 1
    FWIW, clang produces warning: variable 'x' is uninitialized when used within its own initialization [-Wuninitialized]. Commented Apr 9, 2014 at 13:06

2 Answers 2

9

It looks like the warning flags you want are -Wuninitialized -Winit-self (see it live ):

Warn about uninitialized variables that are initialized with themselves. Note this option can only be used with the -Wuninitialized option.

For example, GCC warns about i being uninitialized in the following snippet only when -Winit-self has been specified:

int f()
{
 int i = i;
 return i;
}

This warning is enabled by -Wall in C++.

Based on the comments below there may be some version dependencies. Note, that clang generates a warning for this just using -Wall, which seems more sensible to me:

warning: variable 'x' is uninitialized when used within its own initialization [-Wuninitialized]
int x = x;
 ~ ^

The live example I linked above also includes a commented out clang command line.

Also see Why is -Winit-self separate from -Wuninitialized.

answered Apr 9, 2014 at 13:07
Sign up to request clarification or add additional context in comments.

16 Comments

That doesn't seem to make any difference in this case - did you try it ?
@PaulR added live example from Coliru
Works fine for me, on gcc, only if I use it along with -Wall or along with -Wuninitialized
OK - it doesn't seem to work with my version of gcc.
@ajay I personally feel clang is a better compiler but I have nothing solid to back that up with. It probably helps that I am primarily developing on OS X these days too, and a good percentage of my work is using Objective C.
|
0

Often, with any compiler, aggressive optimisation options will generate warnings not issued during normal compilation, including uninitialised or potentially uninitialised variables in far more complex situations that described in this question.

This is possible due to the execution analysis necessary to perform optimisation. So as a general recommendation, it is worth applying optimisation at the highest possible level (-O3 for example), as a kind of "poor man's static analysis", even if you do not intend to deploy with optimisation at that level.

answered Apr 9, 2014 at 13:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.