문제

I have an example flawed program that should give exactly one warning about an uninitialized variable, but when I compile it gcc doesn't give me any warnings.

Here is the code:

#include <stdio.h>

int main()
{
    int foo;

    printf("I am a number: %d \n", foo);

    return 0;
}

Here is what I run: cc -Wall testcase.c -o testcase

And I get no feedback. As far as I know this should produce:

testcase.c: In function 'main': 
testcase.c:7: warning: 'foo' is used uninitialized in this function

It appears to warn Zed Shaw correctly in a similar example in his C tutorial). This is the example I had first tried and noticed that it wasn't working as expected.

Any ideas?

EDIT:

Version of gcc:

i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
도움이 되었습니까?

해결책

Use Clang, be done with it. Seems like a bug in GCC, cause Clang warns like it should.

다른 팁

Are you compiling with optimisation turned on? Here's what my man gcc page says:

  -Wuninitialized
       Warn if an automatic variable is used without first being
       initialized or if a variable may be clobbered by a "setjmp" call.

      These warnings are possible only in optimizing compilation, because
       they require data flow information that is computed only when
       optimizing.  If you do not specify -O, you will not get these
       warnings. Instead, GCC will issue a warning about -Wuninitialized
       requiring -O.

My version of gcc is:

i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)

Actually, I just tried this on a gcc 4.4.5 and I do get the warning without using -O. So it depends on your compiler version.

Update your compiler.

$ cat test.c
#include <stdio.h>

int main(void)
{
    int foo;
    printf("I am a number: %d \n", foo);
    return 0;
}
$ gcc -Wall -o test ./test.c
./test.c: In function ‘main’:
./test.c:7:11: warning: ‘foo’ is used uninitialized in this function [-Wuninitialized]
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) 
$ 

A compiler is not required by the C Standard to warn when an uninitialized variable is accessed. A compiler is even not required to warn if a program invokes undefined behavior (assuming there is no syntax error and no constraint violations).

With gcc you can enable warnings for uninitialized variables with -Wuninitialized. As others noted, with recent versions of gcc, -Wuninitialized is enabled when -Wall is specified.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top