Hi,
Is there any way to check if there has been an overflow after an assignment?
Like checking the Status register or something.
Thanks..
Hi,
Is there any way to check if there has been an overflow after an assignment?
Like checking the Status register or something.
Thanks..
What kind of assignment? Regular old x=3? [edit] If so you could compare the value casted to the smaller type with the value.
[/edit]Code:if(10000000000L != (short)10000000000L) overflow();
Are you adding something to a variable, x+=3? If so you could try something like this:
If there is such a thing as a "Status register" it isn't part of the ANSI standard.Code:#include <limits.h> short x = SHRT_MAX; if(1 + (long)x > SHRT_MAX) overflow(); x += 1;
[edit=2] This is integer overflow, right? http://en.wikipedia.org/wiki/Integer_overflow [/edit]
Last edited by dwks; 12-23-2006 at 11:53 AM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
It can be done without casting
Code:int x = 6; if(x> INT_MAX - 3) overflow(); else x += 3;
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I knew that . . .
But all the examples you've been given check for overflow before you assign the value. If you've already gone ahead and assigned the value, there's no way to tell if the integer overflowed (unless you have access to the original value, in which case you could check for overflow before the assignment anyway).
The constants in limits.h are very useful for this kind of thing.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Normally, a flag is set in a status register by the processor when an assignment gives an overflow. That's what i 'm telling. I don't want to manually check for overflow.
The question is if i can check it..
Well, that is definitely non-standard. Registers are non-standard. How about stating basic information, like compiler (Turbo C?) and OS (WinXP?) and processor (Intel 32 bit?).
You might try googling, perhaps for "c programming status register integer overflow". The first hit is
http://www.osdata.com/topic/language/asm/register.htm
[edit] From that page:
As you can see, it's very processor-dependant.overflow Set if arithmetic overflow occurs. Used in Digital VAX [V], Intel 80x86 [OF], Motorola 680x0 [V], Motorola 68300 [V], Motorola M68HC16 [V].
It would be better to use one of the standard C methods outlined above if possible. [/edit]
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.