public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Missing warning about uninitialized variable.
@ 2007-11-29 16:01 J.C. Pizarro
  2007-11-29 16:18 ` eschenb
       [not found] ` <C3743DA6.273A8%eljay@adobe.com>
  0 siblings, 2 replies; 31+ messages in thread
From: J.C. Pizarro @ 2007-11-29 16:01 UTC (permalink / raw)
  To: Mikael Vidstedt, gcc-help

On 2007/11/29, Mikael Vidstedt <mikael.vidstedt@bea.com> wrote:
> The following program may make use of an uninitialized variable (gurka):
>
> int
> main(int argc, char* argv[])
> {
>    int gurka;
>
>    if(argc == 10) {
>       gurka = 3;
>    }
>
>    // gurka isn't necessarily initialized here...
>    printf("%d\n", gurka);
>
>    return 0;
> }
>
> GCC 4.0 will give a warning when this program is compiled with "-O
> -Wall". GCC 4.1 and 4.2 do not give that warning. I haven't had the
> possibility to try GCC 4.3.
>
> What say ye?
>
> Thanks,
> Mikael

It prints stochasticly random data too.

gcc version 4.2.3 20071031 (prerelease)

$ gcc -Wall -o foo foo.c
foo.c: In function 'main':
foo.c:11: warning: implicit declaration of function 'printf'
foo.c:11: warning: incompatible implicit declaration of built-in
function 'printf'
$ for i in $(seq 1 5); do ./foo $(seq 1 10) ; done
-1209020420
-1208291332
-1208422404
-1208803332
-1208823812
$

   J.C.Pizarro

^ permalink raw reply	[flat|nested] 31+ messages in thread
* Missing warning about uninitialized variable
@ 2009-11-25 10:09 David Sveningsson
  2009-11-27 14:29 ` Ian Lance Taylor
  0 siblings, 1 reply; 31+ messages in thread
From: David Sveningsson @ 2009-11-25 10:09 UTC (permalink / raw)
  To: gcc-help

Hi, I recently ran into an issue with an uninitialized pointer which I 
expected g++ to warn about.

class Foo {
public:
         Foo* a(){ return this; }
};

int main(int argc, const char*[] ){
         for ( int i = 0; i < 6; i++ ){
                 Foo* foo = foo->a();
         }
}

This code compiles without any warnings (with -Wall) with both g++-4.4.2 
and g++-4.2.4. Removing the for-loop gives me a warning as expected:

foo.cpp: In function ‘int main(int, const char**)’:
foo.cpp:8: warning: ‘foo’ is used uninitialized in this function

I know this case is a bit silly but it happened because of a typo and 
went unnoticed for a while.

Is my reasoning flawed or should g++ emit a warning?

^ permalink raw reply	[flat|nested] 31+ messages in thread
* Missing warning about uninitialized variable
@ 2007-11-29 15:49 Mikael Vidstedt
  2007-11-30 16:31 ` Ian Lance Taylor
  0 siblings, 1 reply; 31+ messages in thread
From: Mikael Vidstedt @ 2007-11-29 15:49 UTC (permalink / raw)
  To: gcc-help


The following program may make use of an uninitialized variable (gurka):

int
main(int argc, char* argv[])
{
   int gurka;

   if(argc == 10) {
      gurka = 3;
   }

   // gurka isn't necessarily initialized here...
   printf("%d\n", gurka);

   return 0;
}

GCC 4.0 will give a warning when this program is compiled with "-O
-Wall". GCC 4.1 and 4.2 do not give that warning. I haven't had the
possibility to try GCC 4.3.

What say ye?

Thanks,
Mikael


# gcc-4.0 --version
gcc-4.0 (GCC) 4.0.4 20060904 (prerelease) (Debian 4.0.3-7)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

# gcc-4.0 -O -Wall -o foo foo.c
foo.c: In function 'main':
foo.c:6: warning: 'gurka' may be used uninitialized in this function
#


# gcc-4.1 --version
gcc-4.1 (GCC) 4.1.3 20071019 (prerelease) (Debian 4.1.2-17)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

# gcc-4.1 -O -Wall -o foo foo.c
#


# gcc-4.2 --version
gcc-4.2 (GCC) 4.2.3 20071014 (prerelease) (Debian 4.2.2-3)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

# gcc-4.2 -O -Wall -o foo foo.c
#

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2009-11-27 14:29 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-29 16:01 Missing warning about uninitialized variable J.C. Pizarro
2007-11-29 16:18 ` eschenb
2007-11-29 16:32   ` J.C. Pizarro
2007-11-29 16:41     ` eschenb
2007-11-29 16:45       ` J.C. Pizarro
2007-11-29 16:48         ` Tom St Denis
2007-11-29 16:57           ` J.C. Pizarro
2007-11-29 16:58             ` Tom St Denis
2007-11-29 18:11               ` J.C. Pizarro
2007-11-29 18:16                 ` Tom St Denis
2007-11-29 18:20                   ` Mikael Vidstedt
2007-11-29 18:30                     ` Tom St Denis
2007-11-29 18:31                       ` Mikael Vidstedt
2007-11-29 21:12                   ` J.C. Pizarro
2007-11-29 21:10                     ` J.C. Pizarro
2007-11-29 21:30                     ` J.C. Pizarro
2007-11-29 21:42                       ` John Love-Jensen
2007-11-29 21:42                         ` Mikael Vidstedt
2007-11-29 22:12                           ` Tony Wetmore
2007-11-29 22:03                         ` J.C. Pizarro
2007-11-29 17:48         ` John Love-Jensen
2007-11-29 18:02           ` J.C. Pizarro
2007-11-29 16:51       ` J.C. Pizarro
2007-11-29 18:18         ` Sven Eschenberg
2007-11-29 18:22           ` Tom St Denis
2007-11-29 18:57             ` Sven Eschenberg
     [not found] ` <C3743DA6.273A8%eljay@adobe.com>
2007-11-29 16:22   ` J.C. Pizarro
  -- strict thread matches above, loose matches on Subject: below --
2009-11-25 10:09 David Sveningsson
2009-11-27 14:29 ` Ian Lance Taylor
2007-11-29 15:49 Mikael Vidstedt
2007-11-30 16:31 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).