Path: csiph.com!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: size_t vs long. Date: Sun, 20 Nov 2022 06:52:00 -0800 Organization: A noiseless patient Spider Lines: 31 Message-ID: <86r0xxadbj.fsf@linuxsc.com> References: <5a71fdad-b7d6-4b4a-a3ee-c5a9beb75d28n@googlegroups.com> <87a64mmzmw.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="a724f5b4ee1d34e760f89c516bf20c9c"; logging-data="3722460"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19PvHVKesfVlQjhAcAJjuU2v7fu7vMWJdo=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:GbAf0PuAZma8yNBBlPua+6UQxyk= sha1:q24OSkXA039NOAJVGkTg/5fil+c= Xref: csiph.com comp.lang.c:168311 James Kuyper writes: > On 11/19/22 15:56, Keith Thompson wrote: > ... > >> I suggest that the most important difference is that size_t is >> guaranteed to be able to hold the size of any object, and long is not. >> That difference is not directly implied by the standard's guarantees >> about their ranges. > > The standard doesn't quite say that about size_t. > > The standard does say that sizeof(type) gives the size of an object of > that type, and that sizeof(expression) gives the size of an object of > the type of that expression. But it's trivial to specify a type for > which the size of that type cannot have a value within range of size_t: > sizeof(char[2][SIZE_MAX]). Such an expression cannot have the behavior > mandated by the C standard for such an expression, but it's less than > perfectly clear how it's wrong. Surely the intention is that such an expression runs afoul of the constraint in 6.6 p4, and also that the program may be rejected by virtue of exceeding a minimum implementation limit. On a 64-bit implementation, try compiling this: printf( "%zu\n", sizeof (char[0xfffffffffffffffe]) ); Both gcc and clang reject this program, complaining about the size implied by the type, even though the size is less than SIZE_MAX (which is 0xffffffffffffffff in that compilation environment).