Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: What is the rank of size_t ?
Date: Wed, 03 Jun 2020 11:33:47 -0700
Organization: A noiseless patient Spider
Lines: 89
Message-ID: <86367cov78.fsf@linuxsc.com>
References: <86pnamrs9r.fsf@linuxsc.com> <86k10rpqum.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="c2e7fdc8253a62623e6cece72591ee6d"; logging-data="7948"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hWW+DOj9k7c2s9HAp5AJOJzR6XE6mARo="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:B0F0HKaxeGwNQu6uCrGjetSg+f0= sha1:FWWlFeC9j1U/Cfhne7GqPKm5AAo=
Xref: csiph.com comp.lang.c:152650
Real Troll writes:
> On 01/06/2020 01:33, Tim Rentsch wrote:
>
>> Real Troll writes:
>>
>>> On 30/05/2020 04:55, Tim Rentsch wrote:
>>>
>>>> Real Troll writes:
>>>>
>>>>> On 27/05/2020 13:49, Spiros Bousbouras wrote:
>>>>>
>>>>>> Specifically , if a has type int and b has type size_t , can
>>>>>> a + b overflow i.e. cause undefined behaviour ?
>>>>>
>>>>> Try running this program to see what happens? I use 64 bit compiler on
>>>>> a 64 bit machine:
>>>>>
>>>>> <******************************************************************>
>>>>>
>>>>> #include
>>>>> #include
>>>>> #include
>>>>> #include
>>>>>
>>>>> int main(void)
>>>>> {
>>>>> const size_t N = 18446744073709551615;
>>>>> int a = 100;
>>>>> size_t result = N + a;
>>>>> printf("SIZE_MAX is: = %zu\n", SIZE_MAX);
>>>>> printf("Value of result (N + a) = %zu\n", result);
>>>>>
>>>>> return 0;
>>>>> }
>>>>>
>>>>> <******************************************************************>
>>>>
>>>> Do you get two program errors? That's what I get.
>>>
>>> No errors in Visual Studio 2019. [...]
>>
>> Well that explains at least some of it. What do you get if
>> you run this:
>>
>> #include
>> #include
>>
>> int
>> main(){
>> printf( "INTMAX_MAX is %td\n", INTMAX_MAX );
>> return 0;
>> }
>>
>> ?
As Ike Naar noted, the conversion sequence %td should instead
have been %jd.
> On a 64 bit machine using 64 bit compiler:
>
> INTMAX_MAX is 9223372036854775807
>
>
> For 32 bit compiler (on 64 bit machine), I was getting errors but I made
> small changes and I got:
>
> INTMAX_MAX is 9223372036854775807
>
> The changes I made were:
>
> printf("INTMAX_MAX is %I64d\n", INTMAX_MAX);
> printf("INTMAX_MAX is %lld\n", INTMAX_MAX);
Oh yes, the famous %I64d. I expect both of these give an
accurate output for the value.
Armed with these new facts, we may conclude the Visual Studio
implementation is not conforming, in two different ways. The
first is that 18446744073709551615 is accepted as a decimal
constant, even though it is larger than 9223372036854775807,
which is the largest value of the largest signed type. Writing
the constant 18446744073709551615 without a trailing u or U
should have been given a diagnostic.
The second is that SIZE_MAX is allowed as a (defined) identifier,
even though has not been included. Only
may define SIZE_MAX. The use of SIZE_MAX without
having been included also merits a diagnostic.