Results 1 to 6 of 6

Thread: Where are my compiler warnings?

  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Where are my compiler warnings?

    I've had to fix several bugs caused by variables not being initialized, so it would be nice if the compiler could issue warnings about that. Normally, it does:
    Qt Code:
    1. int main(int argc, char *[])
    2. {
    3. char *ptr;
    4. char *ptr2;
    5. if (argc==10) ptr="hello";
    6. return int(*ptr + *ptr2);
    7. }
    To copy to clipboard, switch view to plain text mode 

    >g++ -v
    ...
    gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu12)
    >g++ -O2 -Wall -Wextra -Wuninitialized -o dum dum.cc
    dum.cc: In function ‘int main(int, char**)’:
    dum.cc:6: warning: deprecated conversion from string constant to ‘char*’
    dum.cc:7: warning: ‘ptr2’ is used uninitialized in this function
    dum.cc:4: warning: ‘ptr’ may be used uninitialized in this function
    >


    All 3 warnings makes sense: a string constant has type const char * while ptr is char *, the code does not even try to initialize ptr2 and ptr is only maybe initialized.
    The command-line options given to g++ ensure that all warnings are enabled and that optimization is run (which is necessary to detect som types of warnings).
    Let's try to fix the first warning by changing the type of "ptr":

    Qt Code:
    1. int main(int argc, char *[])
    2. {
    3. const char *ptr;
    4. char *ptr2;
    5. if (argc==10) ptr="hello";
    6. return int(*ptr + *ptr2);
    7. }
    To copy to clipboard, switch view to plain text mode 

    >g++ -O2 -Wall -Wextra -Wuninitialized -o dum dum.cc
    dum.cc: In function ‘int main(int, char**)’:
    dum.cc:7: warning: ‘ptr2’ is used uninitialized in this function
    >


    What happened to the warning about ptr perhaps being used uninitialized?

    The problem is not limited to pointers either. This version:

    Qt Code:
    1. int main(int argc, char **)
    2. {
    3. int foo;
    4. if (argc == 10) {
    5. foo = 4711;
    6. }
    7.  
    8. return foo;
    9. }
    To copy to clipboard, switch view to plain text mode 

    gives no warnings at all. I'm suspecting a compiler bug, but would like to know what you guys think before I report it.

  2. #2
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Where are my compiler warnings?

    Weird. I compiled the code on gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20) and got:

    test.cc: In function `int main(int, char**)':
    test.cc:3: warning: `const char*ptr' might be used uninitialized in this
    function
    test.cc:4: warning: `char*ptr2' might be used uninitialized in this function

    Don't have your version installed though, so it may be release-specific. However, my guess is that the compiler has optimized out the const char* ptr reference, and thus does not report it.
    Last edited by gfunk; 17th February 2009 at 20:55.
    Software Engineer



  3. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Where are my compiler warnings?

    I've tried to check what the "const ptr" points to when no codeline assigns a value to it. It seems to point to the "hello" string anyway. Does g++ assign uninitialized "const char *"s to the first string in the data section or something?
    I guess I'd better file a bug-report.

  4. #4
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Where are my compiler warnings?

    Try compiling without the -O2 (which performs level 2 optimizations). It sounds like the code is just being optimized out.
    Software Engineer



  5. #5
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Where are my compiler warnings?

    According to the g++ man page, optimization is required for the compiler to be able to notice uninitialized variables.
    I tried compiling with -O1 and without any -O, in neither case do I get a warning about "ptr may be used initialized".

  6. #6
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Where are my compiler warnings?

    There seems to be a lively discussion about how to handle uninitialized variables: http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings

Similar Threads

  1. QMdiSubWindow - Warnings while compile
    By estanisgeyer in forum Qt Programming
    Replies: 2
    Last Post: 3rd February 2009, 16:37
  2. Useless but curious compiler warning question
    By Raccoon29 in forum General Programming
    Replies: 4
    Last Post: 30th July 2008, 21:46
  3. problem to compile exemple qt4 with xlC compiler
    By poulacou in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2008, 15:59
  4. Replies: 7
    Last Post: 24th March 2007, 14:53
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 23:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.