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. The Lounge
  3. C++ integer arithmetic overflow

C++ integer arithmetic overflow

Scheduled Pinned Locked Moved The Lounge
c++question
25 Posts 19 Posters 1 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.
  • B Offline
    B Offline
    BernardIE5317
    wrote on last edited by
    #1

    may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

    J J J OriginalGriffO P 13 Replies Last reply
    0
    • B BernardIE5317

      may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

      J Offline
      J Offline
      Jeremy Falcon
      wrote on last edited by
      #2

      I code in C, so I'm safe. :-\

      Jeremy Falcon

      M 1 Reply Last reply
      0
      • J Jeremy Falcon

        I code in C, so I'm safe. :-\

        Jeremy Falcon

        M Offline
        M Offline
        Mike Hankey
        wrote on last edited by
        #3

        What could go wrong...right? :)

        I don't think before I open my mouth, I like to be as surprised a everyone else. PartsBin an Electronics Part Organizer - Release Version 1.1.0 JaxCoder.com Latest Article: SimpleWizardUpdate

        1 Reply Last reply
        0
        • B BernardIE5317

          may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

          J Offline
          J Offline
          JudyL_MD
          wrote on last edited by
          #4

          The usual way is after adding two positive numbers together, check if the result is negative. If it is, you've overflowed. You can also, if you're adding two 32-bit numbers together, cast them both to 64-bit, then add them and check if the resulting 64-bit value is greater than INT32_MAX. Judy

          Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

          J U M 3 Replies Last reply
          0
          • B BernardIE5317

            may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

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

            See some suggestions here if you haven't already. https://stackoverflow.com/questions/199333/how-do-i-detect-unsigned-integer-overflow/199455#199455[^]

            "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 "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

            1 Reply Last reply
            0
            • B BernardIE5317

              may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              I only ever use small numbers.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • B BernardIE5317

                may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

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

                It's not a hardware operation?

                1 Reply Last reply
                0
                • B BernardIE5317

                  may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

                  K Offline
                  K Offline
                  k5054
                  wrote on last edited by
                  #8

                  Like OG, I don't use big integers. If I did need big integers, I would probably use a library that provided arbitrarily large numbers - certainly slower than CPU math, but at least 90% of the time acceptable. If you're in that gray area where you might get integer overflow, then you might use some of the techniques suggested on this SO page : [https://stackoverflow.com/questions/199333/how-do-i-detect-unsigned-integer-overflow\](https://stackoverflow.com/questions/199333/how-do-i-detect-unsigned-integer-overflow)

                  Keep Calm and Carry On

                  1 Reply Last reply
                  0
                  • B BernardIE5317

                    may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    Requirements, architecture and design. So no detection but prevention. The business domain can insure that large sets cannot exist. Then from that there might be limited sets with the potential. So one must analyze the problem to insure the following. 1. Can it occur at all? 2. What is the timeline for when it can occur? 1 year? 10 years? 50 years? Myself I do not even attempt to program for long term problems which do not exist now. History does not suggest that doing that leads to better solutions - just ones that are more complex and harder to maintain. If I find that it can occur now then I use a solution that allows for that actually handles large numbers. There are multiple libraries that provide large number handling.

                    1 Reply Last reply
                    0
                    • B BernardIE5317

                      may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

                      R Offline
                      R Offline
                      RedDk
                      wrote on last edited by
                      #10

                      Not here you can't.:thumbsdown:

                      1 Reply Last reply
                      0
                      • B BernardIE5317

                        may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

                        0 Offline
                        0 Offline
                        0x01AA
                        wrote on last edited by
                        #11

                        Q/A would be a more appropriate forum ;)

                        N 1 Reply Last reply
                        0
                        • 0 0x01AA

                          Q/A would be a more appropriate forum ;)

                          N Offline
                          N Offline
                          Nelek
                          wrote on last edited by
                          #12

                          I have to be ill or something like that... :~ I agree with you :rolleyes: :-D :laugh: :laugh:

                          M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                          0 1 Reply Last reply
                          0
                          • N Nelek

                            I have to be ill or something like that... :~ I agree with you :rolleyes: :-D :laugh: :laugh:

                            M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                            0 Offline
                            0 Offline
                            0x01AA
                            wrote on last edited by
                            #13

                            Hi Nelek Yes, you're right, somehow something isn't right anymore, it's changed. This is the second time you agree with me. And this after years of heated discussions I guess it's because I've finally grown up ;P :laugh: ;)

                            N 1 Reply Last reply
                            0
                            • 0 0x01AA

                              Hi Nelek Yes, you're right, somehow something isn't right anymore, it's changed. This is the second time you agree with me. And this after years of heated discussions I guess it's because I've finally grown up ;P :laugh: ;)

                              N Offline
                              N Offline
                              Nelek
                              wrote on last edited by
                              #14

                              0x01AA wrote:

                              I guess it's because I've finally grown up ;-P

                              I would like to think than I have got younger... :laugh: :laugh:

                              M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                              0 1 Reply Last reply
                              0
                              • N Nelek

                                0x01AA wrote:

                                I guess it's because I've finally grown up ;-P

                                I would like to think than I have got younger... :laugh: :laugh:

                                M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                                0 Offline
                                0 Offline
                                0x01AA
                                wrote on last edited by
                                #15

                                No chance for us all to turn back the clock ;)

                                1 Reply Last reply
                                0
                                • J JudyL_MD

                                  The usual way is after adding two positive numbers together, check if the result is negative. If it is, you've overflowed. You can also, if you're adding two 32-bit numbers together, cast them both to 64-bit, then add them and check if the resulting 64-bit value is greater than INT32_MAX. Judy

                                  Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

                                  J Offline
                                  J Offline
                                  jmaida
                                  wrote on last edited by
                                  #16

                                  Many moons ago, I was called in as consultant for a small business using Cobol and random access files for recording inventory transactions. Their main database file had been corrupted with new transactions overwriting existing transactions. I suspected integer oveflow. The field in the header record for the file was used to store the next free location. It was 16 bits so once they reached 65536 transactions the next free location was previously used. The Cobol number field was always positive so the roll over just went backwards. They were very surprised that 65536 transactions had occured. The coding fix was easy. I had to calculate the corrupted locations and restore the data from backups. Because I was still in Grad school, and the owner provided me extra pay for less hours so I could finish. True story.

                                  "A little time, a little trouble, your better day" Badfinger

                                  1 Reply Last reply
                                  0
                                  • J JudyL_MD

                                    The usual way is after adding two positive numbers together, check if the result is negative. If it is, you've overflowed. You can also, if you're adding two 32-bit numbers together, cast them both to 64-bit, then add them and check if the resulting 64-bit value is greater than INT32_MAX. Judy

                                    Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

                                    U Offline
                                    U Offline
                                    User 13269747
                                    wrote on last edited by
                                    #17

                                    Quote:

                                    The usual way is after adding two positive numbers together, check if the result is negative. If it is, you've overflowed.

                                    This doesn't work, because overflowing is undefined behaviour and the compiler is allowed to remove the check (i.e. it will never overflow). See this short snippet[^] The compiler completely removes the "overflow" path, so the check you propose *always* prints "Not overflowed".

                                    1 Reply Last reply
                                    0
                                    • B BernardIE5317

                                      may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

                                      U Offline
                                      U Offline
                                      User 13269747
                                      wrote on last edited by
                                      #18

                                      Quote:

                                      may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

                                      Determining overflow is not possible with signed integers (compiler is free to remove the check on signed integers, but has to keep the check for unsigned integers) so I try to use unsigned over signed wherever possible, and then I can check rollover using less-than/greater-than zero for overflow/underflow respectively. When I am forced to use signed (to interface to existing code), I use a larger integer than the interface specifies: use int16_t for 8 bit ints, int32_t for 16 bit ints, int64_t for 32 bit ints. For an interface that needs 64 bit ints I use the rollover/rollunder zero (using less-than and greater-than) after casting the signed 64 bit value to an unsigned 64 bit value. Under no circumstance is it safe to use plain `int` portably. All usage of plain `int` depends on the platform (hardware + compiler + OS combination), so you cannot write code that works now and in the future, you can only use the plain `int` type for code on a particular platform at a particular point in time - future versions of your platform have have a different sized `int`.

                                      J 1 Reply Last reply
                                      0
                                      • B BernardIE5317

                                        may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

                                        L Offline
                                        L Offline
                                        Lurk
                                        wrote on last edited by
                                        #19

                                        An ounce of prevention is worth a pound of cure. Take either addend and subtract it from the maximum. If the result is smaller than the other addend, you will overflow. If Max - A < B, then A + B > Max // OVERFLOW

                                        M 1 Reply Last reply
                                        0
                                        • U User 13269747

                                          Quote:

                                          may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?

                                          Determining overflow is not possible with signed integers (compiler is free to remove the check on signed integers, but has to keep the check for unsigned integers) so I try to use unsigned over signed wherever possible, and then I can check rollover using less-than/greater-than zero for overflow/underflow respectively. When I am forced to use signed (to interface to existing code), I use a larger integer than the interface specifies: use int16_t for 8 bit ints, int32_t for 16 bit ints, int64_t for 32 bit ints. For an interface that needs 64 bit ints I use the rollover/rollunder zero (using less-than and greater-than) after casting the signed 64 bit value to an unsigned 64 bit value. Under no circumstance is it safe to use plain `int` portably. All usage of plain `int` depends on the platform (hardware + compiler + OS combination), so you cannot write code that works now and in the future, you can only use the plain `int` type for code on a particular platform at a particular point in time - future versions of your platform have have a different sized `int`.

                                          J Offline
                                          J Offline
                                          JSilvers
                                          wrote on last edited by
                                          #20

                                          Sorry for inadvertent click on inappropriate content

                                          Joan F Silverston jsilverston@cox.net nhswinc.com

                                          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