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

  1. #1
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    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
    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, 08:36 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01: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, 03:00 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM