Integer Overflow Corruption

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Salvi
    New Member
    • Feb 2008
    • 9

    Integer Overflow Corruption

    Dear Friend,
    Please view the following question.

    Which one of the following statements is the shortest statement that will prevent integer overflow corruption in the result?
    Choice 1 long c=(long)a*(long )b; ( is not the answer).
    Choice 2 long c=(long)a*b;
    Choice 3 long c=a*b;
    Choice 4 long int c=a*b; [Ans]
    Choice 5 long c=(long)(a*b);

    But I don't how this is said as correct answer.

    I tried to find answer of this question, I found that 1 option is not the answer.
    Kindly say which is answer and why and how....

    Thanks in advance.

    Regards,
    Salvi
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Should we guess what is a and what is b ?

    Comment

    • Salvi
      New Member
      • Feb 2008
      • 9

      #3
      Originally posted by newb16
      Should we guess what is a and what is b ?
      The variables a and b are int.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        If you type "(long)a*b" variable a will be explicitly cast to a long; therefore variable b will be implicitly cast to a long type. The type of the multiplication will also be of type long. I think choice #2 is the answer.

        kind regards,

        Jos

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          I agree with Jos, additionally I find it odd that you have given 4 as the answer since choices 3 and 4 are functionally identical.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by Salvi
            Code:
            int a, b;
            long c=(long)a*(long)b;    // choice 1
            long c=(long)a*b;         // choice 2
            long c=a*b;               // choice 3
            long int c=a*b;          // choice 4
            long c=(long)(a*b);     // choice 5
            In the absence of any casts, arithmetic operations involving all operands of type int, short, or signed char are carried out with int arithmetic.

            In choice 1, a and b are explicitly converted to long before the multiplication takes place, so the multiplication will use long arithmetic. No overflow.

            In choice 2, a is explicitly converted to long before the multiplication takes place, so b is implicitly converted to long and the multiplication will use long arithmetic. No overflow.

            In choice 3, both operands are int, so the multiplication will use int arithmetic. The result of the int multiplication is cast to long. Overflow is possible.

            Choice 4 is indistinguishab le from choice 3 from the compiler's point-of-view.

            In choice 5, you are trying to command the compiler to perform the int multiplication first and then cast the result to long -- the same situation as choice 3. Perhaps there is some loophole in the Standard that allows/requires the cast to take place before the multiplication, but I wouldn't want to count on such counterintuitiv e behavior.

            Comment

            • dzenanz
              New Member
              • Feb 2008
              • 45

              #7
              Donblock said it, although not explicitly, that choices 1 and 2 are equivalent.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                What is the sizeof the long? On 32-bit windows int and long are the same size.

                You can't prevent overflow using this type of coding.

                For multiplication, you divide your multiplicand into the long max-value. This gives you the maximum mumber of multiplicands that will fit in the long. If that number is less than your multipler, there is no overflow. Otherwise, overflow will occur.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Originally posted by weaknessforcats
                  What is the sizeof the long? On 32-bit windows int and long are the same size.
                  Good point. Avoiding arithmetic overflow in this way only works if long is wider than int. You can check that by either comparing sizeof(long) to sizeof(int) or by comparing LONG_MAX to INT_MAX (from limits.h).

                  Comment

                  Working...