public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Bob Plantz <plantz@cds1.net>
Cc: 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: <AANLkTi=OznL9_-pyuT7iRo_F-vuFaeYf6eRO87i4idmQ@mail.gmail.com> (raw)
In-Reply-To: <4D5C437F.5080207@cds1.net>

On 16 February 2011 21:37, 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
>
> which is what I would expect. That is, i is a (signed) char, and when it
> goes over 127 it becomes a negative number, so the loop terminates.

That would be the expected result if char was unsigned. But as I've
already explained up-thread, when a signed integer overflows you get
undefined behaviour.  If you enable optimisation you might get Jason's
result, or you might have demons fly out of your nose, or your hard
drive might get formatted*


* results may vary, empirically I've only seen it wrap to a negative
number or keep getting larger. But I avoid undefined behaviour because
I'm worried about the nasal demons. You should be too.

  reply	other threads:[~2011-02-16 21:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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
2011-02-16 23:00 Bill McEnaney

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='AANLkTi=OznL9_-pyuT7iRo_F-vuFaeYf6eRO87i4idmQ@mail.gmail.com' \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jayrusman@hotmail.com \
    --cc=plantz@cds1.net \
    /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).