From: "Michael Farnham" Newsgroups: comp.os.msdos.djgpp Subject: Re: Lack of overflow problem Date: Fri, 7 Jan 2000 04:38:34 -0600 Organization: Prodigy Internet http://www.prodigy.com Lines: 46 Message-ID: <854fv0$a7bo$1@newssvr03-int.news.prodigy.com> References: <851dtd$bfv$1 AT news DOT luth DOT se> <854cnj$453 AT romeo DOT logica DOT co DOT uk> NNTP-Posting-Host: dll2a010-0115.splitrock.net X-Trace: newssvr03-int.news.prodigy.com 947241760 3966267 209.254.212.115 (7 Jan 2000 10:42:40 GMT) X-Complaints-To: abuse AT prodigy DOT net NNTP-Posting-Date: 7 Jan 2000 10:42:40 GMT X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MSMail-Priority: Normal X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Chris Underwood wrote in message news:854cnj$453 AT romeo DOT logica DOT co DOT uk... > > >Rob McCrea (r_mccrea AT hotmail DOT com) wrote: > >: My expression won't recognize the overflow... > >: > >: while ((s+1) > s) s++; > >: > >: is an infinite loop. What's up with that? > >: Apparently the condition is being evaluted as (1>0). > >: That's not right is it? I'm compiling with gpp -Wall testsize.cpp -o > >: testsize.exe. > >: > >: Please, can someone explain why its not acting like a computer? > > > I'm not sure exactly what you're claiming is wrong here, but you might not > realise that sooner or later, given that 's' is likely an integer of some > size or another, it'll wrap around, and your loop will exit. For example, if > 's' is an unsigned char you will loop until s = 255, then s+1 will be 256 > which overflows and wraps round to 0. (0 > 255) is false, and you'll exit > the loop. > > Chris > > I think that is what was expected. It is possible that the compiler has optimized this loop to while(true) s++;. In the real world s + 1 > s is always true. Sometimes compilers make optimizations that a human being would not make. I have not tested the code and therefore do not know that this is happening. But it is an explanation that fits the observed facts. Regards Mike