Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Signed integers considered harmful?

Signed integers considered harmful?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
14 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Signed integers seem to be a minefield of undefined behaviour lurking around every corner. You can't even add them without potentially blowing up the Death Star, unless you test for potential overflow first. Should they be avoided? How should this be dealt with? How bad is it to pretend it's not a problem?

    C Richard Andrew x64R P J L 6 Replies Last reply
    0
    • L Lost User

      Signed integers seem to be a minefield of undefined behaviour lurking around every corner. You can't even add them without potentially blowing up the Death Star, unless you test for potential overflow first. Should they be avoided? How should this be dealt with? How bad is it to pretend it's not a problem?

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      know your data. check bounds.

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • L Lost User

        Signed integers seem to be a minefield of undefined behaviour lurking around every corner. You can't even add them without potentially blowing up the Death Star, unless you test for potential overflow first. Should they be avoided? How should this be dealt with? How bad is it to pretend it's not a problem?

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        harold aptroot wrote:

        You can't even add them without potentially blowing up the Death Star, unless you test for potential overflow first.

        I'm interested to learn more about this. Can you provide an example of what you mean?

        The difficult we do right away... ...the impossible takes slightly longer.

        L 1 Reply Last reply
        0
        • L Lost User

          Signed integers seem to be a minefield of undefined behaviour lurking around every corner. You can't even add them without potentially blowing up the Death Star, unless you test for potential overflow first. Should they be avoided? How should this be dealt with? How bad is it to pretend it's not a problem?

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          And here I've been wrestling with unsigned longs (in C#) for the last few days... :sigh: Consider:

          ulong max = 1UL << 63 ; // 63 is the maximum of a calculated value

          for ( ulong i = max - 1 ; i < max ; i-- )

          How many people will freak out when they see the test in the for statement?

          1 Reply Last reply
          0
          • L Lost User

            Signed integers seem to be a minefield of undefined behaviour lurking around every corner. You can't even add them without potentially blowing up the Death Star, unless you test for potential overflow first. Should they be avoided? How should this be dealt with? How bad is it to pretend it's not a problem?

            J Offline
            J Offline
            jeron1
            wrote on last edited by
            #5

            Same could be said about unsigned integers.

            "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

            C Richard Andrew x64R L 3 Replies Last reply
            0
            • J jeron1

              Same could be said about unsigned integers.

              "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              or floats, or doubles or any numeric encoding that has to reside on a computer with finite storage. it's all about what you need the numbers to do for you.

              image processing toolkits | batch image processing

              J 1 Reply Last reply
              0
              • C Chris Losinger

                or floats, or doubles or any numeric encoding that has to reside on a computer with finite storage. it's all about what you need the numbers to do for you.

                image processing toolkits | batch image processing

                J Offline
                J Offline
                jeron1
                wrote on last edited by
                #7

                Exactly.

                Chris Losinger wrote:

                it's all about what you need the numbers to do for you.

                Yes it is.

                "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

                1 Reply Last reply
                0
                • J jeron1

                  Same could be said about unsigned integers.

                  "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

                  Richard Andrew x64R Offline
                  Richard Andrew x64R Offline
                  Richard Andrew x64
                  wrote on last edited by
                  #8

                  I was wondering why he singled out signed integers. But now that you point this out, I think I understand what he means.

                  The difficult we do right away... ...the impossible takes slightly longer.

                  P 1 Reply Last reply
                  0
                  • Richard Andrew x64R Richard Andrew x64

                    I was wondering why he singled out signed integers. But now that you point this out, I think I understand what he means.

                    The difficult we do right away... ...the impossible takes slightly longer.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Signed integers lead to singed digits. :-D

                    1 Reply Last reply
                    0
                    • J jeron1

                      Same could be said about unsigned integers.

                      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      No. Unsigned integers wrap, signed integers cause UB. Wrapping can be "not what you wanted", but it does something perfectly predictable and well-defined.

                      1 Reply Last reply
                      0
                      • L Lost User

                        Signed integers seem to be a minefield of undefined behaviour lurking around every corner. You can't even add them without potentially blowing up the Death Star, unless you test for potential overflow first. Should they be avoided? How should this be dealt with? How bad is it to pretend it's not a problem?

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        harold aptroot wrote:

                        Signed integers seem to be a minefield of undefined behaviour

                        Since their behaviour is completely defined, I find it hard to understand what you mean.

                        L 1 Reply Last reply
                        0
                        • Richard Andrew x64R Richard Andrew x64

                          harold aptroot wrote:

                          You can't even add them without potentially blowing up the Death Star, unless you test for potential overflow first.

                          I'm interested to learn more about this. Can you provide an example of what you mean?

                          The difficult we do right away... ...the impossible takes slightly longer.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Not of Death Stars exploding (sadly?). But there's a well-known (and infamous) optimization that GCC does where if on some code path a signed integer would overflow, it deduces that therefore that code path must be dead. In practice this often means that overflow tests that are done after the overflow has already occurred (such as maybe you calculate something, then don't use the result if the calculation overflowed) are deleted, so your program looks correct (after all, you tested for overflows, right?) but isn't.

                          1 Reply Last reply
                          0
                          • L Lost User

                            harold aptroot wrote:

                            Signed integers seem to be a minefield of undefined behaviour

                            Since their behaviour is completely defined, I find it hard to understand what you mean.

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            It isn't. See for example 3.4.3 of C99[^],

                            An example of undefined behavior is the behavior on integer overflow

                            This does not apply to unsigned integers, which can't overflow because they wrap. Taking some other standard doesn't help either, as far as I know that's undefined in all version of C and C++ (but I'd really like to be wrong about that).

                            1 Reply Last reply
                            0
                            • L Lost User

                              Signed integers seem to be a minefield of undefined behaviour lurking around every corner. You can't even add them without potentially blowing up the Death Star, unless you test for potential overflow first. Should they be avoided? How should this be dealt with? How bad is it to pretend it's not a problem?

                              C Offline
                              C Offline
                              CubbiMew
                              wrote on last edited by
                              #14

                              Lots and lots of things in C++ (and C for that matter) can lead to undefined behavior if preconditions are not met. Signed integer arithmetic is just one of many. If you're programming in this language, you should be used to dealing with narrow contracts. So, no, they shouldn't be avoided. Deal with them depending on the situation, in many cases an assert will suffice. Pretending it's not a problem is fatal. (and no, gcc isn't the only compiler that assumes that naive signed overflow checks are always false)

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups