This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: infinite for-loop and related question
- From: "Bill McEnaney" <bill at rkirkpat dot net>
- To: Jonathan Wakely <jwakely dot gcc at gmail dot com>, Jason Mancini <jayrusman at hotmail dot com>, gcc-help at gcc dot gnu dot org
- Date: Wed, 16 Feb 2011 16:00:10 -0700 (MST)
- Subject: Re: infinite for-loop and related question
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