GCC Doesn't Properly Report Access to Uninitialized Pointer Variabl

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It is throwing a warning for a normal variable, but not for a pointer.

#include <stdio.h>

int fun()

{

    return 4;

}

int main()

{

    int x,z;

    int y=fun();

    int *ptr,*p;

    switch(y)

    {

        case 1:

            x = 2;

            break;

        case 2:

            ptr = &x;

//            p = NULL;

            x =3;

            break;

    }

    if ( x== 2)

        printf("x is %d\n",x);

    if ( z== 2)

        printf("z is %d\n",z);

    if ( z== 3)

        printf("z is %d\n",z);

    z = *p;

    if (p != NULL)

    {

        printf("ptr is %d\n",*p);

    }

    if (ptr != NULL)

        printf("ptr is %d\n",*ptr);

    return 0;

}


sdlc2027:~ # gcc -Wall t.c -O
t.c: In function ‘main’:
t.c:24: warning: ‘z’ is used uninitialized in this function
t.c:28: warning: ‘p’ is used uninitialized in this function
t.c:22: warning: ‘x’ may be used uninitialized in this function
sdlc2027:~ #

It doesn't throw any warning for usage of ptr, even though it may also
be used uninitialized.


Is this the expected behavior or bug in gcc? If expected, is there any
way I can make it report warning for these also?


Thanks,

Venkat




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux