help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: detecting arithmetic overflows - Compiler specific flags


From: Karthik Kumar
Subject: Re: detecting arithmetic overflows - Compiler specific flags
Date: Sun, 15 Aug 2004 13:18:55 -0700
User-agent: Mozilla Thunderbird 0.7.3 (Windows/20040803)

Paul Pluzhnikov wrote:
Karthik Kumar <kaykaydreamz_nospamplz@yahoo.com> writes:


   I am writing this application that needs a lot of arithmetic
calculations. I was wondering if the GNU C++ compiler specifies any
way of detecting arithmetic overflows. ( some extensions or flags or
some options).


"The C++ Programming Language", 3rd edition, p.122:

Implementations do not have to check for arithmetic overflow and
hardly any do.


   I was looking at overflow_error class provided by stdexcept
   library in C++. That does not seem to be raised automatically.


Same book, p. 385, lists 'overflow_error' as being thrown *only*
from bitset<>::to_ulong().


How would I handle similar situations ?


You'll have to handle them yourself, e.g.

  inline short mult(short a, short b) {
      int res = a * b;
      if (SHRT_MIN <= res && res <= SHRT_MAX) return res;
      throw overflow_error();
} ...

         short a = -10000;
         short b = mult(a, 8); // now throws


  Thanks Paul for the hints.

- Karthik.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]