infinite for-loop and related question
Jonathan Wakely
jwakely.gcc@gmail.com
Thu Feb 17 12:23:00 GMT 2011
On 17 February 2011 09:44, Axel Freyn <axel-freyn@gmx.de> 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<128.
I'm guessing you didn't bother to actually compile that and test it?
See 5.2.2 [expr.call] paragraph 7 in the C++ standard. Arguments to
varargs functions are subject to integral promotions.
More information about the Gcc-help
mailing list