On Fri, 24 Sep 2004 19:17:10 +0530, Vishal Rao <[EMAIL PROTECTED]> wrote: > Hi, > > How to detect arithmetic overflow/underflow in C and C++? > > For example, if I do something like this: > > unsigned i = 0; > i--;
new value is i is 4294967295 (max value of unsigned). > > Whats a good way to detect/catch/trap the underflow? > > Right now I do something like: > > unsigned iPrev = i; > i--; > if (iPrev<i) return MYERROR_UNDERFLOW; Whenever there is increment, store that value in iPrev. and Before increment, check new value is greater than iPrev. Can't understand your exact prob. So maybe this is vague answer. Why not write a function ModifyVariable(newValue,oldValue) which will take care of all necessary things? > > Cheers, > Vishal > -- ______________________________________________________________________ Pune GNU/Linux Users Group Mailing List: ([EMAIL PROTECTED]) List Information: http://plug.org.in/mailing-list/listinfo/plug-mail Send 'help' to [EMAIL PROTECTED] for mailing instructions.
