From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10035 invoked by alias); 16 Feb 2011 20:17:48 -0000 Received: (qmail 10026 invoked by uid 22791); 16 Feb 2011 20:17:47 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW X-Spam-Check-By: sourceware.org Received: from mail-iw0-f175.google.com (HELO mail-iw0-f175.google.com) (209.85.214.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 16 Feb 2011 20:17:43 +0000 Received: by iwn8 with SMTP id 8so1719154iwn.20 for ; Wed, 16 Feb 2011 12:17:41 -0800 (PST) MIME-Version: 1.0 Received: by 10.42.241.70 with SMTP id ld6mr1570973icb.124.1297887461849; Wed, 16 Feb 2011 12:17:41 -0800 (PST) Received: by 10.42.230.68 with HTTP; Wed, 16 Feb 2011 12:17:41 -0800 (PST) In-Reply-To: References: Date: Wed, 16 Feb 2011 20:41:00 -0000 Message-ID: Subject: Re: infinite for-loop and related question From: Jonathan Wakely To: Jason Mancini Cc: gcc-help@gcc.gnu.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2011-02/txt/msg00242.txt.bz2 On 16 February 2011 20:09, Jason Mancini wrote: > > Hello, > So as I recall, the following can be an infinite loop now with optimizati= ons, right? > > =A0 for (int i(1); i!=3D0; ++i) { ... } Right. > What about: > > =A0 unsigned int x =3D 0xFFFFFFFFU; > =A0 x =3D x+1; > =A0 if (x) { ... can we get here because "positive x + 1 must still posit= ive"? ... } > > 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.