Path: csiph.com!weretis.net!feeder6.news.weretis.net!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: "Catch-23: The New C Standard,Sets the World on Fire" by Terence Kelly with Special Guest Borer Yekai Pan
Date: Wed, 12 Apr 2023 05:46:34 -0700
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <86a5zd2rpx.fsf@linuxsc.com>
References: <874jpv84uv.fsf@bsb.me.uk> <87edoxhmgr.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="00dd14a5b0fe7fcb33b04c058bf64644"; logging-data="3193326"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Mamj893D6FbjfdOBY8SL3Lg/ynA7SZ3A="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:JMrqQ/RsNeOG+PyU1RLHaQ+vD2w= sha1:AhttJSzVsnMN62H2z7BwbjGNQWk=
Xref: csiph.com comp.lang.c:169998
Keith Thompson writes:
> Under C89/C90 rules, realloc(ptr, 0) definitely frees the object
> pointed to by ptr (assuming it had been allocated properly). The
> standard doesn't clearly say what realloc() returns in that case.
The C90 standard does say what realloc() returns in that case (of
course whether it says so clearly is a subjective question). The
return value is dictated by the two penultimate sentences in the
general description at the beginning of section 7.10.3, "Memory
management functions":
[...] If the space cannot be allocated, a null pointer is
returned. If the size of the space requested is zero, the
behavior is implementation-defined; the value returned shall
be either a null pointer or a unique pointer. [...]
That description is consistent with the 'Returns' portion of
section 7.10.3.4 "The realloc function", which says:
The realloc function returns either a null pointer or a
pointer to the possibly moved allocated space.
> Common sense suggests that it behaves like malloc(0), so it
> returns either a null pointer or a unique pointer (and the choice
> is implementation-defined, so it must be documented).
I don't think any common sense is needed. There is nothing in the
description under 7.10.3.4 that contradicts the general description
given in 7.10.3, so that specification must be followed.
Incidentally, note that it is always possible for realloc(ptr,0) to
return a non-null value (when ptr is non-null), because if nothing
else the original value of ptr could be returned, to be the 'unique
pointer'. The same is true for any other call to realloc() when
the requested size is less than or equal to the current size.