1

I am writing a code that do some arithmetic on long values and it should NOT generate an overflow exception.

The code needs to be compiled both on windows (visual studio ) and Linux (ARM Linux).

Is there any pragma or compile time directive that I can use to tell the compile that it should not generate an overflow exception for that specific code.

The main point here is:

1- It works both on windows and Linux

2- Irrespective of the general configuration of project, this section should not generate an overflow exception. (so setting a parameter in project is not a good solution).

mans
  • 17,104
  • 45
  • 172
  • 321
  • 1
    I'm pretty sure there are no exceptions (in the meaning of C++ exception) when an overflow occurs. – Davidbrcz Feb 17 '14 at 11:42
  • In practice, integer multiplication won't generate an exception. C/C++ require that *unsigned* integer multiplication not overflow, if you want a guarantee. – Brett Hale Feb 17 '14 at 11:49
  • @BrettHale Thanks. What about signed integer? Does it generate overflow? – mans Feb 17 '14 at 11:51
  • It's technically undefined behaviour. Which means a correct C/C++ program should *not* result in a signed overflow. Which in turn means you should avoid such an operation, or use unsigned arithmetic. That said, 'undefined behaviour' may include 'do nothing' - x86[-64] for example. – Brett Hale Feb 17 '14 at 12:01
  • @BrettHale 'undefined behavior' my include 'give wrong results' as well; in fact, if the results don't fit in the target type, it's practically impossible for a compiler to do anything else (except raise an exception or a signal of some sort, but such implementations are rare). – James Kanze Feb 17 '14 at 12:14
  • @JamesKanze - I think that's implicit in my comment. And most architectures will set an overflow flag that can be queried - but will not raise an exception. – Brett Hale Feb 17 '14 at 12:19
  • @BrettHale The overflow flag cannot be queried in C++. – James Kanze Feb 17 '14 at 12:21
  • @JamesKanze - I said the 'architecture'. You are being deliberately obtuse. – Brett Hale Feb 17 '14 at 12:22

1 Answers1

0

I'm not aware of any implementation which will generate an overflow exception for expressions with integral type. But the question is: what do you want it to do. All of the Windows and Linux compilers I know will simply silently give the wrong results, and I can't think of a situation where this would be preferable to any alternative.

The normal way of handling this sort of problem is by using pre-condition checks, before starting the calculations. You know the calculations which are going to be done, so you can determine the range of values which can safely be handled without overflow, and you verify the input is in that range before starting.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • What I want is to the overflow data is lost. So for example if I have byte x=128, byte y=129, then byte c=x+y, C should be 1 and there should be no exception. – mans Feb 17 '14 at 16:54
  • That's the rule for unsigned arithmetic. But why? Why shoud `c` be `1`? – James Kanze Feb 17 '14 at 19:21