public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Bill McEnaney" <bill@rkirkpat.net>
To: Jonathan Wakely <jwakely.gcc@gmail.com>,
	Jason Mancini <jayrusman@hotmail.com>,
	gcc-help@gcc.gnu.org
Subject: Re: infinite for-loop and related question
Date: Wed, 16 Feb 2011 23:00:00 -0000	[thread overview]
Message-ID: <20110216230010.531D571842@saratoga.rkirkpat.net> (raw)

Although I doubt anyone would write it, this would work, wouldn't it?

int n = +10;

while ( n > 0)
   n += -1;

Bill

> On 16 February 2011 20:09, Jason Mancini wrote:
> >
> > Hello,
> > So as I recall, the following can be an infinite loop now with optimizations, right?
> >
> >   for (int i(1); i!=0; ++i) { ... }
> 
> Right.
> 
> > What about:
> >
> >   unsigned int x = 0xFFFFFFFFU;
> >   x = x+1;
> >   if (x) { ... can we get here because "positive x + 1 must still positive"? ... }
> >
> > If not, given the first, why not?
> 
> No.  The C and C++ standards define that unsigned integers do not
> overflow, they wrap, with well-defined behaviour.
> 
> They do not define what happens if a signed integer overflows, so your
> first loop results in undefined behaviour, and so you cannot
> reasonably expect any particular behaviour. The compiler can do
> whatever it likes with your code.
> 
> Put another way:
> There is no way for a correct C or C++ program to increment a signed
> integer greater than zero such that the result is zero. Because a
> correct C or C++ program does not contain integer overflows.
> 
> 

________________________________________________________________
Please visit a saintly hero:
http://www.jakemoore.org

             reply	other threads:[~2011-02-16 23:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-16 23:00 Bill McEnaney [this message]
  -- strict thread matches above, loose matches on Subject: below --
2011-02-16 20:17 Jason Mancini
2011-02-16 20:41 ` Jonathan Wakely
2011-02-16 21:37   ` Jason Mancini
2011-02-16 21:49     ` Bob Plantz
2011-02-16 23:00       ` Jonathan Wakely
2011-02-16 23:22         ` Thomas Martitz
2011-02-17  6:31           ` Jonathan Wakely
2011-02-17 10:36       ` Axel Freyn
2011-02-17 12:23         ` Jonathan Wakely
2011-02-17 13:16           ` Axel Freyn
2011-02-17 14:08             ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110216230010.531D571842@saratoga.rkirkpat.net \
    --to=bill@rkirkpat.net \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jayrusman@hotmail.com \
    --cc=jwakely.gcc@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).