From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21137 invoked by alias); 17 Feb 2011 13:09:50 -0000 Received: (qmail 21020 invoked by uid 22791); 17 Feb 2011 13:09:49 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from mailout-de.gmx.net (HELO mailout-de.gmx.net) (213.165.64.22) by sourceware.org (qpsmtpd/0.43rc1) with SMTP; Thu, 17 Feb 2011 13:09:43 +0000 Received: (qmail invoked by alias); 17 Feb 2011 13:09:40 -0000 Received: from LN-mac29.grenoble.cnrs.fr (EHLO axel) [147.173.67.29] by mail.gmx.net (mp032) with SMTP; 17 Feb 2011 14:09:40 +0100 Date: Thu, 17 Feb 2011 13:16:00 -0000 From: Axel Freyn To: gcc-help@gcc.gnu.org Subject: Re: infinite for-loop and related question Message-ID: <20110217130938.GT5274@axel> Mail-Followup-To: gcc-help@gcc.gnu.org References: <4D5C437F.5080207@cds1.net> <20110217094436.GQ5274@axel> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.18 (2008-05-17) 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/msg00258.txt.bz2 On Thu, Feb 17, 2011 at 11:36:53AM +0000, Jonathan Wakely wrote: > On 17 February 2011 09:44, Axel Freyn wrote: > > On Wed, Feb 16, 2011 at 01:37:03PM -0800, Bob Plantz wrote: > >> On 02/16/2011 12:41 PM, Jason Mancini wrote: > >>> Though I still find the output of this odd: > >>> > >>>    for (char i(1); i>0; ++i) > >>>      printf("%d %d\n", i, sizeof(i)); > >>> > >>> ... > >>> 362195 1 > >>> 362196 1 > >>> 362197 1 > >>> ... > >>> > >>> For very large values of char!  ^_^ > >>> > >>> Jason > >> That's odd. With g++ 4.4.5 on an x86-64 machine in 64-bit mode I get: > >> > >> --- > >> 125 1 > >> 126 1 > >> 127 1 > > But there is also a second "bug" in the program. You tell "printf", that > > the variable "i" is a integer and not a char -- so printf will read > > sizeof(int) Bytes at the adress of "i" in order to create the > > output-number, which gives you the 362195.  You should write something > > like > >      printf("%d %d\n", (int)i, sizeof(i)); > > in order to get the "true" value of i in the output -- then I would > > expect values in the "char"-range -127 > I'm guessing you didn't bother to actually compile that and test it? Well, I did. But using gcc instead of g++. I get values <128 as soon as I add the integer conversion (in C code). > See 5.2.2 [expr.call] paragraph 7 in the C++ standard. Arguments to > varargs functions are subject to integral promotions. In C++ it does not make any difference. Axel