2

Given

int x = a number;
int y = some number;
int z = x + y;  // did overflow occur?

At least on Intel chips, overflow detection is possible through the overflow flag. But there's no direct way to get to it through ANSI C. Why not?

NOTE: The question is not about how to detect overflow while using ANSI C/C++. Rather, it's asking why didn't Dennis Ritchie, and later the ANSI C committee, provide this very useful feature.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Curious
  • 128
  • 5
  • 4
    i guess there is at least one processor that does not allow overflow detection – 463035818_is_not_an_ai Apr 29 '17 at 09:46
  • 1
    Possible duplicate of [How to detect integer overflow in C/C++?](http://stackoverflow.com/questions/199333/how-to-detect-integer-overflow-in-c-c) – Henri Menke Apr 29 '17 at 10:43
  • @HenriMenke: Nope. I'm aware of that thread, as well as how to detect overflow within ANSI C/C++. I'm asking for the reason why Dennis Ritchie, and subsequently the ANSI C standardization committee did not provide for it. – Curious Apr 29 '17 at 15:25
  • @tobi303: a) that's what I was guessing, but a friend contradicted me. So, if that's the answer, then a relevant link is what is required; b) Probably a separate question, but if your guess is correct, why didn't the committee require a simulation of the overflow flag by compiler writers when porting to such processors? Here, the acceptable answer would have to be more detailed than that it would be expensive. – Curious Apr 29 '17 at 15:28
  • 2
    This question is difficult to answer in any definitive way - it's like trying to prove a negative. It's basically going to require someone to stumble upon some official discussion of this, which may well not exist. – Oliver Charlesworth Apr 30 '17 at 16:21
  • @OliverCharlesworth: a) Hopefully, there are people from the committee lurking around stack overflow; b) I could introduce a series of other questions that try to triangulate an answer to this. I'll wait a few days till the holiday is over. – Curious Apr 30 '17 at 16:36
  • actually it's not available in most programming languages – phuclv Mar 24 '23 at 07:51
  • @phuclv: No doubt, but C is just one step above assembly, so one would expect more low-level features from it than from most other languages. – Curious Mar 25 '23 at 20:09

0 Answers0