Show compiler warning/error when passing int although unsigned int is expected

This is a discussion on Show compiler warning/error when passing int although unsigned int is expected within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, consider the following code example: Code: void func(unsigned int _u) { //do a lot of stuff } int ...

  1. #1
    PiJ
    PiJ is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    9

    Show compiler warning/error when passing int although unsigned int is expected

    Hello everyone,

    consider the following code example:

    Code:
    void func(unsigned int _u)
    {
    //do a lot of stuff
    }
    
    int main()
    {
    int i = -1;
    func(i);
    return 0;
    }
    Is there any way to make the compiler write a warning or an error, when an int is passed to a function which awaits an unsigned int? I tried some of the options gcc gives the user, but nothing worked for me.

    I'm using gcc, version 4.1.3 20070929 on an Ubuntu system.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    You're passing a signed integer (-1 can't be represented in an unsigned integer!) where an unsigned one is expected, which is a bad idea.
    PiJ is asking how to get the compiler to warn about such conversions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Oops. Sorry about that. I misread.
    But it's weird... why wouldn't the compiler complain? I tried on Visual Studio and got the same result. No warning or error.
    Last edited by Elysia; 02-13-2008 at 09:49 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I can't find a way to get gcc to do that. I tried -Wall -Wextra -pedantic and also -Wconversion.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    I can't find a way to get gcc to do that. I tried -Wall -Wextra -pedantic and also -Wconversion.
    I think that is because signed to unsigned integer conversions are defined in the C++ Standard, so the compiler assumes that it is intended.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Another horrible implicit conversion, then? This makes us wish there was a way to stop implicit conversion at specific places in code, beyond constructors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    that's why I like PC-Lint
    Code:
    void f(unsigned int i)
    {
    	i++;
    }
    
    int main(void)
    {
       int i = -1;
       f(i);
    }
    Code:
    --- Module:   str2d.c (C)
    _
    }
    str2d.c  4  Warning 550: Symbol 'i' (line 1) not accessed
    str2d.c  1  Info 830: Location cited in prior message
       _
    f(i);
    str2d.c  9  Info 732: Loss of sign (arg. no. 1) (int to unsigned int)
        _
    }
    
    str2d.c  10  Info 783: Line does not end with new-line
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Elysia View Post
    Another horrible implicit conversion, then? This makes us wish there was a way to stop implicit conversion at specific places in code, beyond constructors.
    You can use integer container types to enforce your own conversion rules. With a good compiler, it doesn't even produce inefficient code.

  9. #9
    PiJ
    PiJ is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    9
    After a little more research using the gcc mailing list I got an answer to my question:

    http://gcc.gnu.org/wiki/NewWconversion

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It doesn't sound to me as if it will actually give a warning for the above scenario still, since there is no loss of data in the conversin of int to unsigned int - they have the same number of bits, the only difference comes when you start using the value for comparisons - in all other aspects, it's the same.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    PiJ
    PiJ is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    9
    To my mind it does give a warning for the explained scenario.

    Following the link from above, approximately in the middle of the page is an example of what is intended to be supported in the future:

    Here the code example:
    http://gcc.gnu.org/viewcvs/trunk/gcc...?revision=HEAD

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by PiJ View Post
    To my mind it does give a warning for the explained scenario.

    Following the link from above, approximately in the middle of the page is an example of what is intended to be supported in the future:

    Here the code example:
    http://gcc.gnu.org/viewcvs/trunk/gcc...?revision=HEAD
    Yes, I think you are right.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    It doesn't sound to me as if it will actually give a warning for the above scenario still, since there is no loss of data in the conversin of int to unsigned int - they have the same number of bits, the only difference comes when you start using the value for comparisons - in all other aspects, it's the same.

    --
    Mats
    On a bit level, yes they are the same, but on a numeric meaning they are very different, and the loss of data is the negative number flag. I've seen VC++ give warnings about signed/unsigned comparisons, so I can't see why it wouldn't warn you about this? Is this in the C++ standard because of some legacy C crap, or did they actually think it was a good idea?

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm all for having a warning for passing signed int into a unsigned int function, but it seemed from the description (I didn't study the test-cases in detail) that it wouldn't do that.

    After all, if someone is getting hords of warnings on existing code, they can always turn off that PARTICULAR warning.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 05:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21