Find the answer to your Linux question:
Results 1 to 7 of 7
Hi All, I'm trying to get rid of a bunch of warning messages when I compile and most of them boil down to warning me about comparing an unsigned int ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Location
    Brockville, Ontario, Canada
    Posts
    74

    Suppress warning about unsigned int compared with int


    Hi All,
    I'm trying to get rid of a bunch of warning messages when I compile and most of them boil down to warning me about comparing an unsigned int with a regular int.

    For example, if I'm looping with a line like:
    for(int i = 0; i < someVector.size(); i++)
    ....

    I can go around to ever <vector>.size() call and explicity cast the unsigned int to an int, but there must be a better way than this.

    What am I missing?

    Thanks

    Stu

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Use -Wno-sign-compare

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Why not declare i as unsigned?

  4. #4
    Just Joined!
    Join Date
    Apr 2005
    Location
    Brockville, Ontario, Canada
    Posts
    74
    I went with the no-sign-compare option for now...I have too many for loops where I use int so it was just easier to add that option, although I guess I could find myself in some stragnge trouble down the road.

    Thanks for the help.

    Stu

  5. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Why do you declare i in the for loop? Just make it global through the function and you've only got a single instance to change! Or get a decent editor and make a global change with a single clever command?

    It's good prgramming practice to match datatypes, BTW, casting (explicit or implicit) is (IMO) A Bad Thing.

  6. #6
    Just Joined!
    Join Date
    Apr 2005
    Location
    Brockville, Ontario, Canada
    Posts
    74
    Yeah, it was the need to cast types that I thought wasn't the greatest idea and resulted in me posting.

    My convention is to not have global for loop integers because if I'm in a situation where there are 3, 4, 5 different loops within a single scope it seems safer to me that I can't accidentally improperly re-use a variable from a wider scope... do you know what I mean. Am I misunderstanding you?

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by sadohert
    Yeah, it was the need to cast types that I thought wasn't the greatest idea and resulted in me posting.

    My convention is to not have global for loop integers because if I'm in a situation where there are 3, 4, 5 different loops within a single scope it seems safer to me that I can't accidentally improperly re-use a variable from a wider scope... do you know what I mean. Am I misunderstanding you?
    No, my suggestion didn't cover the case of nested loops. Whenever I use nested loops I use different loop counter names because I usually have to refer to all of them in the innermost loop! It's a good idea to limit scope where you can.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •