delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/07/08:11:18

From: "Chris Underwood" <underwoodc AT logica DOT nospam DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Lack of overflow problem
Date: Fri, 7 Jan 2000 09:47:17 -0000
Organization: Logica UK Ltd.
Lines: 24
Message-ID: <854cnj$453@romeo.logica.co.uk>
References: <dhwa4.274$Gh2 DOT 1735 AT newsfeed DOT slurp DOT net> <851dtd$bfv$1 AT news DOT luth DOT se>
NNTP-Posting-Host: 158.234.18.170
X-Newsreader: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

>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


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019