   Data Overflow
  How do you detect if a data type has been overflown in C?  For example, you have a program that is adding integers together, how do I tell if adding two certain integers will be a result bigger then "int" can hold?ThanksJames   
  You can't.I believe that Visual Studio C++ supports some overflow checking, but I won't guarantee for which types, and how rigid.Google however returns some helpful information.   ;  You can't test for overflow post operation, but if you design it into your system I think that something like this will work:int result = BIG_NUMBER;int additive = 100000;if(result + additive < result){  // overflow has occurred.}else{  result += additive;}This will only work if additive is always positive.  I leave it as an exercise for the reader to know how to write the negative version.  And, of course, sorry if this is in fact bad information, it's late and I have a headache   ;  For unsigned numbers it's even easier than what nobodynews posted. Simply check if the answer is less than one of the operands:unsigned int a, b, c;cin >> a >> b;c = a + b;if (c < a){    // Overflow}For signed integers it's a little harder (as shown by nobodynews), but not much.I'm surprised that page Antheus linked to makes no mention of this extremely simple technique for unsigned types.   ;Quote:Original post by iMalcFor unsigned numbers it's even easier than what nobodynews posted. Simply check if the answer is less than one of the operands:*** Source Snippet Removed ***For signed integers it's a little harder (as shown by nobodynews), but not much.I'm surprised that page Antheus linked to makes no mention of this extremely simple technique for unsigned types.Because as the site statesQuote:... because in the C language, when adding a number to a pointer or to a signed integer, overflow is undefined. Unfortunately, gcc as of version 4.1 abuses this and optimizes away checks that would only be true if there was an overflow, in particular checks that check for an overflow.;Quote:Original post by dragongameQuote:Original post by iMalcI'm surprised that page Antheus linked to makes no mention of this extremely simple technique for unsigned types.Because as the site statesQuote:... because in the C language, when adding a number to a pointer or to a signed integer, overflow is undefined. Unfortunately, gcc as of version 4.1 abuses this and optimizes away checks that would only be true if there was an overflow, in particular checks that check for an overflow.I did read that, but since neither 'a', nor 'b' are a pointer or signed integer in the code I posted, I'm sure that doesn't apply. Particularly because they went to the trouble of saying 'signed' in that article (emphasis mine), suggesting that unsigned is fine. If not, then it is an omission for them to not mention unsigned as well.This makes sense anyway, because on a non-two's complement system you would most highly likely get a different result for overflow. But for an unsigned type, I can't imagine that possibility.Can anyone with a copy of the standard confirm this?   ;  Dont know about C, but in C++ I would use boost::numeric_cast.   ;  FWIW, the Standard does make a fair number more guarantees about unsigned types than it does about signed types.   ;Quote:Original post by iMalcCan anyone with a copy of the standard confirm this?Yes, the C standard requires that unsigned integer overflow must wrap around and behave as if the underlying representation is two's complement, even if it isn't.  Things like linear congruential random number generators depend implicitly on this mandated behaviour.The overflow behaviour is undefined because different hardware does different things (some just "wrap around", some raise hardware exceptions).  GCC certainly does not abuse undefined behaiour by implementing a particular solution.  Optimizing away code or reformatting your hard drive are both acceptable responses to undefined behaviour.--smw   