Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

"Just think of a computer as hardware you can program." -- Nigel de la Tierre


devel / comp.lang.c / Re: *rubeyes*: realloc(ptr, 0) is UB?

SubjectAuthor
* *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
+* Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
|`* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
| `* Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
|  `* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
|   `- Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
+* Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
|`* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
| `- Re: *rubeyes*: realloc(ptr, 0) is UB?BGB
+* Re: *rubeyes*: realloc(ptr, 0) is UB?Scott Lurndal
|+* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Scott Lurndal
|| +- Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
|| `* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||  `* Re: *rubeyes*: realloc(ptr, 0) is UB?Scott Lurndal
||   +* Re: *rubeyes*: realloc(ptr, 0) is UB?James Kuyper
||   |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   | `* Re: *rubeyes*: realloc(ptr, 0) is UB?James Kuyper
||   |  `* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |   +- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |   `* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    +* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    | `* Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    |  +- Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    |  `* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    |   `* Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    |    `* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    |     +* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    |     |`- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    |     `* Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    |      `* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    |       `* Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    |        `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    +* Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
||   |    |+* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    ||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    || `* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    ||  `* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    ||   `* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    ||    +* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    ||    |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    ||    | `* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    ||    |  `* Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
||   |    ||    |   `- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    ||    +* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    ||    |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    ||    | `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    ||    `- Re: *rubeyes*: realloc(ptr, 0) is UB?James Kuyper
||   |    |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    | +* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    | |+* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    | ||`- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    | |+* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||+- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    | ||+* Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | |||+* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||||+* Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
||   |    | |||||+* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    | ||||||`- Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
||   |    | |||||+* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    | ||||||+* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    | |||||||`* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||||||| `- Re: *rubeyes*: realloc(ptr, 0) is UB?Malcolm McLean
||   |    | ||||||`* Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
||   |    | |||||| `* Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
||   |    | ||||||  `- Re: *rubeyes*: realloc(ptr, 0) is UB?David Brown
||   |    | |||||+- Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | |||||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||||| `- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||||+* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    | |||||`- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | |||| +* Re: *rubeyes*: realloc(ptr, 0) is UB?Malcolm McLean
||   |    | |||| |`- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | |||| `* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||||  +- Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||||  `- Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
||   |    | |||`- Re: *rubeyes*: realloc(ptr, 0) is UB?Lew Pitcher
||   |    | ||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    | || `* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||  +* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    | ||  |+- Re: *rubeyes*: realloc(ptr, 0) is UB?Kalevi Kolttonen
||   |    | ||  |+* Re: *rubeyes*: realloc(ptr, 0) is UB?bart
||   |    | ||  ||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||  || `- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||  |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Scott Lurndal
||   |    | ||  | `- Re: *rubeyes*: realloc(ptr, 0) is UB?Chris M. Thomasson
||   |    | ||  `- Re: *rubeyes*: realloc(ptr, 0) is UB?Richard Kettlewell
||   |    | |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    | | `* Re: *rubeyes*: realloc(ptr, 0) is UB?Malcolm McLean
||   |    | |  +- Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    | |  +- Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    | |  +* Re: *rubeyes*: realloc(ptr, 0) is UB?Scott Lurndal
||   |    | |  |`- Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
||   |    | |  +- Re: *rubeyes*: realloc(ptr, 0) is UB?Spiros Bousbouras
||   |    | |  `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    | `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    +* Re: *rubeyes*: realloc(ptr, 0) is UB?Keith Thompson
||   |    |+* Re: *rubeyes*: realloc(ptr, 0) is UB?Lawrence D'Oliveiro
||   |    ||`* Re: *rubeyes*: realloc(ptr, 0) is UB?Kaz Kylheku
||   |    |`* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   |    `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
||   `- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
|`* Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch
+* Re: *rubeyes*: realloc(ptr, 0) is UB?Blue-Maned_Hawk
`- Re: *rubeyes*: realloc(ptr, 0) is UB?Tim Rentsch

Pages:1234567
Re: *rubeyes*: realloc(ptr, 0) is UB?

<wwvmssxteq0.fsf@LkoBDZeT.terraraq.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49085&group=comp.lang.c#49085

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.niel.me!nntp.terraraq.uk!.POSTED.tunnel.sfere.anjou.terraraq.org.uk!not-for-mail
From: invalid@invalid.invalid (Richard Kettlewell)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 18:17:27 +0000
Organization: terraraq NNTP server
Message-ID: <wwvmssxteq0.fsf@LkoBDZeT.terraraq.uk>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<87msszbb08.fsf@nosuchdomain.example.com> <86cytugvve.fsf@linuxsc.com>
<87edeab85u.fsf@nosuchdomain.example.com>
<wwvv87lwzuo.fsf@LkoBDZeT.terraraq.uk>
<8734upbbf1.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: innmantic.terraraq.uk; posting-host="tunnel.sfere.anjou.terraraq.org.uk:172.17.207.6";
logging-data="15918"; mail-complaints-to="usenet@innmantic.terraraq.uk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:IBp6XhNo315gbG4hZRKo/Z/of9U=
X-Face: h[Hh-7npe<<b4/eW[]sat,I3O`t8A`(ej.H!F4\8|;ih)`7{@:A~/j1}gTt4e7-n*F?.Rl^
F<\{jehn7.KrO{!7=:(@J~]<.[{>v9!1<qZY,{EJxg6?Er4Y7Ng2\Ft>Z&W?r\c.!4DXH5PWpga"ha
+r0NzP?vnz:e/knOY)PI-
X-Boydie: NO
 by: Richard Kettlewell - Mon, 22 Jan 2024 18:17 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> Richard Kettlewell <invalid@invalid.invalid> writes:
>> Fully agreed. That permission has been grit in the gears for a very long
>> time, for much of which I had the misfortune of having to deal with it
>> in real life thanks to IBM’s bad decisions.
>
> Can you expand on "IBM's bad decisions"?

AIX is one of the platforms that returns NULL for malloc(0) even when
there’s some memory available (in fact the only one I know).

--
https://www.greenend.org.uk/rjk/

Re: *rubeyes*: realloc(ptr, 0) is UB?

<_GyrN.373000$83n7.225494@fx18.iad>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49086&group=comp.lang.c#49086

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx18.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Newsgroups: comp.lang.c
References: <20240116162506.143@kylheku.com> <86r0ifjbiw.fsf@linuxsc.com> <QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me> <20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <87msszbb08.fsf@nosuchdomain.example.com> <86cytugvve.fsf@linuxsc.com> <87edeab85u.fsf@nosuchdomain.example.com> <wwvv87lwzuo.fsf@LkoBDZeT.terraraq.uk> <8734upbbf1.fsf@nosuchdomain.example.com> <wwvmssxteq0.fsf@LkoBDZeT.terraraq.uk>
Lines: 23
Message-ID: <_GyrN.373000$83n7.225494@fx18.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 22 Jan 2024 18:43:06 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 22 Jan 2024 18:43:06 GMT
X-Received-Bytes: 2181
 by: Scott Lurndal - Mon, 22 Jan 2024 18:43 UTC

Richard Kettlewell <invalid@invalid.invalid> writes:
>Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>> Richard Kettlewell <invalid@invalid.invalid> writes:
>>> Fully agreed. That permission has been grit in the gears for a very long
>>> time, for much of which I had the misfortune of having to deal with it
>>> in real life thanks to IBM’s bad decisions.
>>
>> Can you expand on "IBM's bad decisions"?
>
>AIX is one of the platforms that returns NULL for malloc(0) even when
>there’s some memory available (in fact the only one I know).

AIX had this odd 'overcommit' feature, which allowed the system to
overcommit memory resources. It would allocate virtual address space
(via malloc) even if there wasn't enough memory + backing store to
support it, and would take a SIGSEGV when referenced if, at the time
of reference, there was still insufficient memory and/or backing store
to support allocating a physical page to that portion of the VA space.

Linux can be configured to operate in the same way.

I don't believe AIX ever returned NULL from malloc() while still
allocating VA space for the allocation.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<20240122105055.336@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49087&group=comp.lang.c#49087

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 18:54:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <20240122105055.336@kylheku.com>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<20240118185544.347@kylheku.com> <865xzmj4bv.fsf@linuxsc.com>
<wwvh6j5mzxv.fsf@LkoBDZeT.terraraq.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 18:54:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="019b7810e586565adbc643fa2cd39f06";
logging-data="910102"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lNP4lljkR6GQl+vyg+nrfg+XnbBn4KyU="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:c0bxlNroRFhxNKdkee8Twi2DPDI=
 by: Kaz Kylheku - Mon, 22 Jan 2024 18:54 UTC

On 2024-01-22, Richard Kettlewell <invalid@invalid.invalid> wrote:
> Standard C is trying to have its own cake and eat it here: 0-sized
> allocations can be represented by null pointers when it’s malloc, but
> not when it’s memcpy.

Having your cake and eating that same cake it is undefined behavior,

even with an intervening sequence point.
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<wwvfrypyyhm.fsf@LkoBDZeT.terraraq.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49088&group=comp.lang.c#49088

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!nntp.terraraq.uk!.POSTED.tunnel.sfere.anjou.terraraq.org.uk!not-for-mail
From: invalid@invalid.invalid (Richard Kettlewell)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 19:11:33 +0000
Organization: terraraq NNTP server
Message-ID: <wwvfrypyyhm.fsf@LkoBDZeT.terraraq.uk>
References: <20240116162506.143@kylheku.com> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<87msszbb08.fsf@nosuchdomain.example.com> <86cytugvve.fsf@linuxsc.com>
<87edeab85u.fsf@nosuchdomain.example.com>
<wwvv87lwzuo.fsf@LkoBDZeT.terraraq.uk>
<8734upbbf1.fsf@nosuchdomain.example.com>
<wwvmssxteq0.fsf@LkoBDZeT.terraraq.uk>
<_GyrN.373000$83n7.225494@fx18.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: innmantic.terraraq.uk; posting-host="tunnel.sfere.anjou.terraraq.org.uk:172.17.207.6";
logging-data="16889"; mail-complaints-to="usenet@innmantic.terraraq.uk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:Buyi038tImElEOy3qcp9+k3yz9Y=
X-Face: h[Hh-7npe<<b4/eW[]sat,I3O`t8A`(ej.H!F4\8|;ih)`7{@:A~/j1}gTt4e7-n*F?.Rl^
F<\{jehn7.KrO{!7=:(@J~]<.[{>v9!1<qZY,{EJxg6?Er4Y7Ng2\Ft>Z&W?r\c.!4DXH5PWpga"ha
+r0NzP?vnz:e/knOY)PI-
X-Boydie: NO
 by: Richard Kettlewell - Mon, 22 Jan 2024 19:11 UTC

scott@slp53.sl.home (Scott Lurndal) writes:
> Richard Kettlewell <invalid@invalid.invalid> writes:

>>AIX is one of the platforms that returns NULL for malloc(0) even when
>>there’s some memory available (in fact the only one I know).
>
> AIX had this odd 'overcommit' feature, which allowed the system to
> overcommit memory resources. It would allocate virtual address space
> (via malloc) even if there wasn't enough memory + backing store to
> support it, and would take a SIGSEGV when referenced if, at the time
> of reference, there was still insufficient memory and/or backing store
> to support allocating a physical page to that portion of the VA space.
>
> Linux can be configured to operate in the same way.

Indeed, Linux defaults to overcommit for most allocations.

> I don't believe AIX ever returned NULL from malloc() while still
> allocating VA space for the allocation.

I don’t really see the relevance. malloc(0) is NULL on AIX[1] because
that’s what they’ve chosen to do in the C library’s malloc
implementation, nothing to do with the kernel’s overcommit policies.

[1] I see from the man page that they’ve added a macro to enable more
sensible behaviour. Too late for us, we stopped supporting it years
ago.

--
https://www.greenend.org.uk/rjk/

Re: *rubeyes*: realloc(ptr, 0) is UB?

<LSFRupcoMc1QvVkCJ@bongo-ra.co>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49089&group=comp.lang.c#49089

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.org!news.nntp4.net!paganini.bofh.team!not-for-mail
From: spibou@gmail.com (Spiros Bousbouras)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 19:23:54 -0000 (UTC)
Organization: To protect and to server
Message-ID: <LSFRupcoMc1QvVkCJ@bongo-ra.co>
References: <20240116162506.143@kylheku.com> <QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com> <QfbqN.230722$c3Ea.54531@fx10.iad>
<uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <uoem42$39ohi$2@dont-email.me>
<877ck5c8dl.fsf@nosuchdomain.example.com> <20240119134055.699@kylheku.com> <861qaaj3k9.fsf@linuxsc.com>
<uolngi$nke1$2@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 19:23:54 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="1401037"; posting-host="9H7U5kayiTdk7VIdYU44Rw.user.paganini.bofh.team"; mail-complaints-to="usenet@bofh.team"; posting-account="9dIQLXBM7WM9KzA+yjdR4A";
Cancel-Lock: sha256:YQqMHSP/qboVJ2kQWoL+8AOrsRJucF9CW6GgHmTW+xg=
X-Notice: Filtered by postfilter v. 0.9.3
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
 by: Spiros Bousbouras - Mon, 22 Jan 2024 19:23 UTC

On Mon, 22 Jan 2024 12:36:34 +0000
Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> No, because it's natural to write
>
> employees = malloc(Nemployees * sizeof(EMPLOYEE));
> if (!employees)
> goto out_of_memory;
>
> You don't want to have to special case Nemployees == 0.

I cannot think of a realistic application where you would not have to treat
as a special case Nemployees == 0 anyway for reasons completely unrelated
to the behaviour of malloc() .Code (of a realistic application) referring to
employees would need to do something involved and your code above would be
part of that greater involved code so that code should not be called at all
if Nemployees == 0 .

Re: *rubeyes*: realloc(ptr, 0) is UB?

<20240122105444.667@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49090&group=comp.lang.c#49090

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 19:36:03 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <20240122105444.667@kylheku.com>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk> <uoljea$n2pc$1@dont-email.me>
Injection-Date: Mon, 22 Jan 2024 19:36:03 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="019b7810e586565adbc643fa2cd39f06";
logging-data="926954"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+qgiP1EtCQm/LBotJ/wFdgoisl84d1340="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:y+pbHrJvRv3nKRWeDPasI4bon2c=
 by: Kaz Kylheku - Mon, 22 Jan 2024 19:36 UTC

On 2024-01-22, bart <bc@freeuk.com> wrote:
> On 22/01/2024 10:22, Richard Kettlewell wrote:
>> I think the design of malloc got this one right.
>
> I think it got it wrong. Now everyone is lumbered with an allocation
> scheme that will ALWAYS have to store the size of that struct, perhaps
> taking as much space as the struct itself.

Implementations of malloc have special strategies for small objects,
to allocate them compactly, with minimal overhead and fragmentation.
It is not necessary to store the size in every such object.

> Imagine allocating 100M of those structs, and also storing 100M copies
> of sizeof(Date) or whatever metadata is needed.

For the size to take up as much object as the struct itself, it
has to be a struct that whose size is sizeof(size_t), which is tiny:
often 8 bytes nowadays, or possibly 4.

An object that is as small as size_t should just be passed around
by value. It likely fits into a machine register, and so dynamically
allocating it is inefficient.

Every such allocation results in a pointer. The program has to retain at
least one copy of that pointer. The pointer is probably also 8 bits
wide, so even if those objects are allocated compactly without storing a
size field, it takes double the space just to retain pointers to them.

> Getting around that, by writing your own small-object allocator on top
> of malloc, is a lot harder that adding your own size-tracking on top of
> a malloc that does not store any extra data. As I've shown.

Writing small-object allocators on malloc is mostly a fool's errand,
in the absence of special justification.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uomjsr$su3k$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49094&group=comp.lang.c#49094

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: kalevi@kolttonen.fi (Kalevi Kolttonen)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 20:40:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 9
Sender: <untosten@0.0.0.0>
Message-ID: <uomjsr$su3k$1@dont-email.me>
References: <20240116162506.143@kylheku.com> <QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me> <20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com> <20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me> <wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk> <uoljea$n2pc$1@dont-email.me> <20240122105444.667@kylheku.com>
Injection-Date: Mon, 22 Jan 2024 20:40:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="995de9be5a1a600bc0ee856e663a907d";
logging-data="948340"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OrFlEcSljyLMs7cNGWCL1/stH9/KtXNc="
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.6.11-200.fc39.x86_64 (x86_64))
Cancel-Lock: sha1:ohp6yXP5LEYhtp8+0hyjdTBNkxg=
 by: Kalevi Kolttonen - Mon, 22 Jan 2024 20:40 UTC

Kaz Kylheku <433-929-6894@kylheku.com> wrote:
> [...] Every such allocation results in a pointer. The
> program has to retain at least one copy of that pointer.
> The pointer is probably also 8 bits wide, so [...]

Surely you meant to write 8 *bytes* wide.

br,
KK

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uomp1g$tokp$4@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49099&group=comp.lang.c#49099

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 22:08:49 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <uomp1g$tokp$4@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uokeuk$e718$1@dont-email.me> <uoll3q$nbib$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 22:08:49 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bfdaa5dc2469429165b2d062b928a109";
logging-data="975513"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zq1zfWvCxuAqx7Y6F27Or"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:ls5MLPUsH/MDN/p48w5P6Iuq36Q=
 by: Lawrence D'Oliv - Mon, 22 Jan 2024 22:08 UTC

On Mon, 22 Jan 2024 11:55:38 +0000, bart wrote:

> Then on Windows I might get:
>
> ...
>
> Some odd results ...

.... but you repeat yourself.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uomp3e$tokp$5@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49100&group=comp.lang.c#49100

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 22:09:50 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <uomp3e$tokp$5@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uok0ah$c5dj$1@dont-email.me> <87il3mbcfy.fsf@nosuchdomain.example.com>
<uol8nr$l82b$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 22:09:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bfdaa5dc2469429165b2d062b928a109";
logging-data="975513"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GQiF9hXzp08eU7bB/Dgla"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:8l6m59vzUEwZImOk1kZ33GRhshI=
 by: Lawrence D'Oliv - Mon, 22 Jan 2024 22:09 UTC

On Mon, 22 Jan 2024 09:24:26 +0100, David Brown wrote:

> On 21/01/2024 22:31, Keith Thompson wrote:
>
>> I suspect that calling malloc() with one size and free() with a
>> different one would have been a rich source of subtle bugs.
>>
>>
> Sure. But that's part of the fun of C :-)

Not so much fun for the poor users who paid you the big bucks and expected
to get working code in return.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uompcq$tsij$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49101&group=comp.lang.c#49101

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 22:14:51 +0000
Organization: A noiseless patient Spider
Lines: 71
Message-ID: <uompcq$tsij$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk> <uoljea$n2pc$1@dont-email.me>
<20240122105444.667@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 22:14:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e1e6f23a5b67b4655f7a372c9f27f255";
logging-data="979539"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pYFIHfKQiHjHJCGgEQrRrz0LjxeZqAAo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kS1oMQKCj2eZKkfXXjmmHbJB8w8=
In-Reply-To: <20240122105444.667@kylheku.com>
Content-Language: en-GB
 by: bart - Mon, 22 Jan 2024 22:14 UTC

On 22/01/2024 19:36, Kaz Kylheku wrote:
> On 2024-01-22, bart <bc@freeuk.com> wrote:
>> On 22/01/2024 10:22, Richard Kettlewell wrote:
>>> I think the design of malloc got this one right.
>>
>> I think it got it wrong. Now everyone is lumbered with an allocation
>> scheme that will ALWAYS have to store the size of that struct, perhaps
>> taking as much space as the struct itself.
>
> Implementations of malloc have special strategies for small objects,
> to allocate them compactly, with minimal overhead and fragmentation.
> It is not necessary to store the size in every such object.
>
>> Imagine allocating 100M of those structs, and also storing 100M copies
>> of sizeof(Date) or whatever metadata is needed.
>
> For the size to take up as much object as the struct itself, it
> has to be a struct that whose size is sizeof(size_t), which is tiny:
> often 8 bytes nowadays, or possibly 4.
>
> An object that is as small as size_t should just be passed around
> by value. It likely fits into a machine register, and so dynamically
> allocating it is inefficient.
>
> Every such allocation results in a pointer. The program has to retain at
> least one copy of that pointer. The pointer is probably also 8 bits
> wide, so even if those objects are allocated compactly without storing a
> size field, it takes double the space just to retain pointers to them.

You keep talking about all the clever, space-efficient ways that malloc
/could/ manage memory. Unfortunately the people who write the mallocs I
use haven't got the memo.

If I go back to the program I posted earlier and allocate 8 bytes at a
time, then successive values from malloc are 32 bytes apart (when
allocations are clearly consecutive).

If my struct actually needs 32 bytes, then it will allocate 48 bytes: a
50% overhead.

Do you have an actual transcript from your machine where it does
anything different?

--------------------------
#include <stdio.h>
#include <stdlib.h>

enum {n=32};

int main(void) {
char* lastp=malloc(n);
char* p;
for (int i=0; i<10; ++i) {
p=malloc(n);
printf("%p %zu\n", p, p-lastp);
lastp=p;
}
} --------------------------

>> Getting around that, by writing your own small-object allocator on top
>> of malloc, is a lot harder that adding your own size-tracking on top of
>> a malloc that does not store any extra data. As I've shown.
>
> Writing small-object allocators on malloc is mostly a fool's errand,
> in the absence of special justification.
>

On the Binary Tree benchmark, using a small-object allocator on top of
malloc makes it three times as fast as just using malloc.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uomqpn$ttrm$2@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49104&group=comp.lang.c#49104

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 14:38:46 -0800
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <uomqpn$ttrm$2@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk> <uoljea$n2pc$1@dont-email.me>
<20240122105444.667@kylheku.com> <uompcq$tsij$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 22:38:47 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f5efac9e35c211335209a589d0078cec";
logging-data="980854"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18a5jZ686M4AC2Szm7JJ+96cZNdAZo7XOI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:+SzOqWBjlDS4pgW6tEiIKtxpSOg=
In-Reply-To: <uompcq$tsij$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 22 Jan 2024 22:38 UTC

On 1/22/2024 2:14 PM, bart wrote:
> On 22/01/2024 19:36, Kaz Kylheku wrote:
>> On 2024-01-22, bart <bc@freeuk.com> wrote:
>>> On 22/01/2024 10:22, Richard Kettlewell wrote:
>>>> I think the design of malloc got this one right.
[...]
>
> On the Binary Tree benchmark, using a small-object allocator on top of
> malloc makes it three times as fast as just using malloc.

Fwiw, it is not unheard of to use malloc to implement a scheme that can
free things by rounding an address down to a large boundary, where
access to a header is right there... Basically, avoid as many calls to
malloc as you can. The over head is that all memory allocations must be
at least the size of a word where sizeof(word) == sizeof(word*). This
pointer is used to link memory into linked lists whose heads are
contained in the header sitting on a large alignment boundary. One time
I put the head right before the boundary. To get at the header:

free would round address down and subtract the size of the header to get
at it. The free could link the freed memory into a FIFO list or
something. Sometimes the header would have a region allocator as well...

If the allocator had to free a aligned main block, it could just free it
using free. Per thread blocks were also heavily used. So, wrapping up
malloc/free can be a good thing, indeed!

:^)

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uomqsa$ttrm$3@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49105&group=comp.lang.c#49105

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 14:40:09 -0800
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <uomqsa$ttrm$3@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk> <uoljea$n2pc$1@dont-email.me>
<20240122105444.667@kylheku.com> <uompcq$tsij$1@dont-email.me>
<uomqpn$ttrm$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 22:40:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f5efac9e35c211335209a589d0078cec";
logging-data="980854"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+l3uz4brcxqa59W5rBNc48pPxvhXGBWMA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:JEMxm5kSKhXH5Us9pj96ZKsj9b4=
In-Reply-To: <uomqpn$ttrm$2@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 22 Jan 2024 22:40 UTC

On 1/22/2024 2:38 PM, Chris M. Thomasson wrote:
> On 1/22/2024 2:14 PM, bart wrote:
>> On 22/01/2024 19:36, Kaz Kylheku wrote:
>>> On 2024-01-22, bart <bc@freeuk.com> wrote:
>>>> On 22/01/2024 10:22, Richard Kettlewell wrote:
>>>>> I think the design of malloc got this one right.
> [...]
>>
>> On the Binary Tree benchmark, using a small-object allocator on top of
>> malloc makes it three times as fast as just using malloc.
>
> Fwiw, it is not unheard of to use malloc to implement a scheme that can
> free things by rounding an address down to a large boundary, where
> access to a header is right there... Basically, avoid as many calls to
> malloc as you can. The over head is that all memory allocations must be
> at least the size of a word where sizeof(word) == sizeof(word*). This
> pointer is used to link memory into linked lists whose heads are
> contained in the header sitting on a large alignment boundary. One time
> I put the head right before the boundary. To get at the header:
>
> free would round address down and subtract the size of the header to get
> at it. The free could link the freed memory into a FIFO list or
> something. Sometimes the header would have a region allocator as well...
>
> If the allocator had to free a aligned main block, it could just free it
> using free. Per thread blocks were also heavily used. So, wrapping up
> malloc/free can be a good thing, indeed!
>
> :^)

Basically use standard malloc/free as a slow path..... ;^)

Re: *rubeyes*: realloc(ptr, 0) is UB?

<9eCrN.252977$PuZ9.141471@fx11.iad>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49106&group=comp.lang.c#49106

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx11.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Newsgroups: comp.lang.c
References: <20240116162506.143@kylheku.com> <QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me> <20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com> <20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me> <wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk> <uoljea$n2pc$1@dont-email.me> <20240122105444.667@kylheku.com>
Lines: 32
Message-ID: <9eCrN.252977$PuZ9.141471@fx11.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 22 Jan 2024 22:45:25 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 22 Jan 2024 22:45:25 GMT
X-Received-Bytes: 2519
 by: Scott Lurndal - Mon, 22 Jan 2024 22:45 UTC

Kaz Kylheku <433-929-6894@kylheku.com> writes:
>On 2024-01-22, bart <bc@freeuk.com> wrote:
>> On 22/01/2024 10:22, Richard Kettlewell wrote:
>>> I think the design of malloc got this one right.
>>
>> I think it got it wrong. Now everyone is lumbered with an allocation
>> scheme that will ALWAYS have to store the size of that struct, perhaps
>> taking as much space as the struct itself.
>
>Implementations of malloc have special strategies for small objects,
>to allocate them compactly, with minimal overhead and fragmentation.
>It is not necessary to store the size in every such object.
>
>> Imagine allocating 100M of those structs, and also storing 100M copies
>> of sizeof(Date) or whatever metadata is needed.
>
>For the size to take up as much object as the struct itself, it
>has to be a struct that whose size is sizeof(size_t), which is tiny:
>often 8 bytes nowadays, or possibly 4.
>
>An object that is as small as size_t should just be passed around
>by value. It likely fits into a machine register, and so dynamically
>allocating it is inefficient.

Given the alignment requirements associated with various
application binary interfaces, malloc must generally
return an address aligned to an ABI specified boundary
(often 16-byte). Such an allocator will often round the allocation
size up to the next 16-byte boundary.

"The malloc() and calloc() functions return a pointer to the
allocated memory that is suitably aligned for any kind of variable"

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uomrm9$u6ed$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49107&group=comp.lang.c#49107

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 14:54:00 -0800
Organization: A noiseless patient Spider
Lines: 61
Message-ID: <uomrm9$u6ed$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk> <uoljea$n2pc$1@dont-email.me>
<20240122105444.667@kylheku.com> <9eCrN.252977$PuZ9.141471@fx11.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 22:54:01 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f5efac9e35c211335209a589d0078cec";
logging-data="989645"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+k8vYKIpUDookKybzQko544EdQjwhn1q4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:wSkXc+l/dgzrY0Y6KnjBTLZyEd4=
Content-Language: en-US
In-Reply-To: <9eCrN.252977$PuZ9.141471@fx11.iad>
 by: Chris M. Thomasson - Mon, 22 Jan 2024 22:54 UTC

On 1/22/2024 2:45 PM, Scott Lurndal wrote:
> Kaz Kylheku <433-929-6894@kylheku.com> writes:
>> On 2024-01-22, bart <bc@freeuk.com> wrote:
>>> On 22/01/2024 10:22, Richard Kettlewell wrote:
>>>> I think the design of malloc got this one right.
>>>
>>> I think it got it wrong. Now everyone is lumbered with an allocation
>>> scheme that will ALWAYS have to store the size of that struct, perhaps
>>> taking as much space as the struct itself.
>>
>> Implementations of malloc have special strategies for small objects,
>> to allocate them compactly, with minimal overhead and fragmentation.
>> It is not necessary to store the size in every such object.
>>
>>> Imagine allocating 100M of those structs, and also storing 100M copies
>>> of sizeof(Date) or whatever metadata is needed.
>>
>> For the size to take up as much object as the struct itself, it
>> has to be a struct that whose size is sizeof(size_t), which is tiny:
>> often 8 bytes nowadays, or possibly 4.
>>
>> An object that is as small as size_t should just be passed around
>> by value. It likely fits into a machine register, and so dynamically
>> allocating it is inefficient.
>
> Given the alignment requirements associated with various
> application binary interfaces, malloc must generally
> return an address aligned to an ABI specified boundary
> (often 16-byte). Such an allocator will often round the allocation
> size up to the next 16-byte boundary.
>
> "The malloc() and calloc() functions return a pointer to the
> allocated memory that is suitably aligned for any kind of variable"

Fwiw, then there is this little region allocator "thing" I made, that
can be fed with memory from malloc, iirc in this example it is using
stack memory... It does use some hack(s), but it does properly align
things to their lowest alignment requirements. It was a while back:

Link to raw text:

https://pastebin.com/raw/f37a23918

Where I "think" I first mentioned it:

https://groups.google.com/g/comp.lang.c/c/7oaJFWKVCTw/m/sSWYU9BUS_QJ

An interesting macro in here is:
________________
#define RALLOC_ALIGN_OF(mp_type) \
offsetof( \
struct { \
char pad_RALLOC_ALIGN_OF; \
mp_type type_RALLOC_ALIGN_OF; \
}, \
type_RALLOC_ALIGN_OF \
)
________________

;^D

Re: *rubeyes*: realloc(ptr, 0) is UB?

<wwv7ck1arzf.fsf@LkoBDZeT.terraraq.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49109&group=comp.lang.c#49109

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.nntp4.net!nntp.terraraq.uk!.POSTED.tunnel.sfere.anjou.terraraq.org.uk!not-for-mail
From: invalid@invalid.invalid (Richard Kettlewell)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 23:05:56 +0000
Organization: terraraq NNTP server
Message-ID: <wwv7ck1arzf.fsf@LkoBDZeT.terraraq.uk>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me>
<877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<wwvbk9dmzvk.fsf@LkoBDZeT.terraraq.uk> <uoljea$n2pc$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: innmantic.terraraq.uk; posting-host="tunnel.sfere.anjou.terraraq.org.uk:172.17.207.6";
logging-data="20221"; mail-complaints-to="usenet@innmantic.terraraq.uk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:TCgpXbLn/nbU5TA+E7tJutuLPb0=
X-Face: h[Hh-7npe<<b4/eW[]sat,I3O`t8A`(ej.H!F4\8|;ih)`7{@:A~/j1}gTt4e7-n*F?.Rl^
F<\{jehn7.KrO{!7=:(@J~]<.[{>v9!1<qZY,{EJxg6?Er4Y7Ng2\Ft>Z&W?r\c.!4DXH5PWpga"ha
+r0NzP?vnz:e/knOY)PI-
X-Boydie: NO
 by: Richard Kettlewell - Mon, 22 Jan 2024 23:05 UTC

bart <bc@freeuk.com> writes:
> Imagine allocating 100M of those structs, and also storing 100M copies
> of sizeof(Date) or whatever metadata is needed.

I’m comfortable with that. It’s a general-purpose interface, it doesn’t
have to be the most efficient conceivable option in any particular
specialized use case.

--
https://www.greenend.org.uk/rjk/

Re: *rubeyes*: realloc(ptr, 0) is UB?

<pan$bb552$50ca4b2e$1541d619$e7097d14@invalid.invalid>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49111&group=comp.lang.c#49111

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!bluemanedhawk.eternal-september.org!.POSTED!not-for-mail
From: bluemanedhawk@invalid.invalid (Blue-Maned_Hawk)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 23:12:09 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <pan$bb552$50ca4b2e$1541d619$e7097d14@invalid.invalid>
References: <20240116162506.143@kylheku.com>
<pan$eed53$d5a25673$c4eed852$f5f44c0@invalid.invalid>
<20240117160615.197@kylheku.com>
<pan$b42aa$a1ca5f31$3fabcad$7e6ed64a@invalid.invalid>
<20240118144637.733@kylheku.com>
<pan$a4d49$9d845f9f$d4df4789$9fb1b256@invalid.invalid>
<20240119083826.926@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Jan 2024 23:12:09 -0000 (UTC)
Injection-Info: bluemanedhawk.eternal-september.org; posting-host="e80390565b479469cf127d80eac58218";
logging-data="986781"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1852LG6fAe/tEIulC4Rq3clvzPXJrYqwG4="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:B9HzW2tbAKydpOCr8iTIsI9x5j8=
X-Face: Llanfair­pwllgwyngyllÃ
ƒ‚Ã
ƒƒ‚­gogery­chwyrnÃ
ƒƒ
? ?‚­drobwll­llan
Ã
? ?ƒƒ‚­tysilio­go
g
o­goch
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAIAAADYYG7QAAACh0lEQVRYw71Z21bD
MAzzevbfkr4cHjrSXJyL044+MDa6WLEl2SkvkrZ1AbAvXO+bUGSCPYnsuIVGMpm
ZLnjX718GhAKNsp8lON2F9VrhELwIgJlBepkZjA78rVK+FkmNhEJK76UsJlz8+E
rJsjrpYouhLo/SC6qPHgakFOR8wV9+8rCfO/I/oVnmUZUp42/LW2XkLj9TCFNM9
jp5g2EmHZgpYZjCOkYU7sXVogRylJqpdggoFLG1g09Flah/7kErCxzR9HgXPYsq
0glb9cxjIz2Vsk9AmAoCSxECpD713joMKjQqLAtmMqJmXjdVvlMnMQCVITotJd1
z+fh1f1NNo+vuc1KnhWUmY7t03vydTud9BbXCtN3L2PL3bK7JCNG0GHzuZxafyB
fxevCxpm1vrwZltqw6SILCcdoCE6PGQC8wZWDA9Or7Qp5s3lAZezys0nDazs9S9
R0TjwEiksRxLkNPC1NMMWPs1bj0Ei0Yuo+JVtFLuzP1NRJ16qXWN8DhhtmS4PDg
O6mqRxs4bEJrYt087mSIow/1VzW2oFlMQuiuIy/KsUagvhdw6hSjJGlIavbLF8x
j3X47bccLcUSi0dkWh1nUZNhANT1tHKUXrNxNLbd9KPb9wDDVrKwmPQMOPQ1oy6
k5I1DwzDeRJd3jVIhDAUxq3ngzJG4CCkNXZxZVMcjefoK2J0gUY2S3rxz/RuTFx
2zHd9U+obimJXMG4edsk/2j5pTU5G1MmzbRLxkfq5EiT1GGsidvMGzi+1goGb2l
GCrN+nGnV8xj3q3JLRDVPL96vUc7Z4aJ3TN1mVqWAMJMfG+Jxh6TQqP+92iZkCU
xtglds1AB6r0aiSHKcnFck+p/c/0CbacFLQcajGcAAAAASUVORK5CYII=
 by: Blue-Maned_Hawk - Mon, 22 Jan 2024 23:12 UTC

Kaz Kylheku wrote:

> On 2024-01-19, Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> wrote:
>> Kaz Kylheku wrote:
>>> Of course, that's not what "behavior can be readily inferred" means,
>>> no matter where I got the example.
>>
>> Inferences may be makeäble, but they have no power.
>
> Yeah, tell that to your compiler when it surprisingly deletes code based
> on inference.

Apologies, let me clarify: In a technical standard, inferences from
nonnormative text cannot be used to make normative statements.

--
Blue-Maned_Hawk│shortens to
Hawk│/
blu.mɛin.dÊ°ak/
│he/him/his/himself/Mr.
blue-maned_hawk.srht.site

Re: *rubeyes*: realloc(ptr, 0) is UB?

<86plxsg0k9.fsf@linuxsc.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49122&group=comp.lang.c#49122

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17687@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 20:01:42 -0800
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <86plxsg0k9.fsf@linuxsc.com>
References: <20240116162506.143@kylheku.com> <QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com> <9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com> <QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me> <20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <20240118185544.347@kylheku.com> <865xzmj4bv.fsf@linuxsc.com> <wwvh6j5mzxv.fsf@LkoBDZeT.terraraq.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="c985c2e759848cd31734101b28e08031";
logging-data="1198051"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+RNUZWINTyfz9G4W+F1jKPzRVU7G45aM0="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:4nhBFeOAGFaUzpGEDMqM7FqwJGQ=
sha1:rA4+HUdup6qkYcsSI87WpmcI9KE=
 by: Tim Rentsch - Tue, 23 Jan 2024 04:01 UTC

Richard Kettlewell <invalid@invalid.invalid> writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> That's a half-assed argument. There are other ways a pointer might
>> have a null value than just being the result of a call to malloc().
>> If code might call memset() et al with a zero size and a null
>> pointer, it's better to address all possible cases at once rather
>> than just some of them:
>>
>> static inline void *
>> safer_memset( void *s, int c, size_t n ){
>> return n ? memset( s, c, n ) : s;
>> }
>>
>> static inline void *
>> safer_memcpy( void *d, const void *s, size_t n ){
>> return n ? memcpy( d, s, n ) : d;
>> }
>>
>> /* ... etc ... */
>
> Of course that's what the cautious programmer must do practice. But in
> terms of the total cost (over all users, implementers, etc) fixing the
> definitions of memcpy/memset/etc (as well as malloc) would have been the
> better answer.

Better in some ways, worse in others. Better for me is not
always the same as better for thee.

> Standard C is trying to have its own cake and eat it here: 0-sized
> allocations can be represented by null pointers when it's malloc, but
> not when it's memcpy.

Actually the two decisions have essentially nothing to do with
each other. You might want to read what the C Rationale
document has to say about the decisions behind various
memory allocation policies.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<86le8gg08t.fsf@linuxsc.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49123&group=comp.lang.c#49123

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17687@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 20:08:34 -0800
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <86le8gg08t.fsf@linuxsc.com>
References: <20240116162506.143@kylheku.com> <QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com> <9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com> <QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me> <20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <87msszbb08.fsf@nosuchdomain.example.com> <86cytugvve.fsf@linuxsc.com> <87edeab85u.fsf@nosuchdomain.example.com> <wwvv87lwzuo.fsf@LkoBDZeT.terraraq.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="c985c2e759848cd31734101b28e08031";
logging-data="1198051"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18sMNoT2IlDPcgrbJd38wFW4iJFQ+9QM/o="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:hOTStsq3J69Nzi1byQmuKxsfx10=
sha1:bxvUClz3Ft1Ai1U3N4mU2qE8CqQ=
 by: Tim Rentsch - Tue, 23 Jan 2024 04:08 UTC

Richard Kettlewell <invalid@invalid.invalid> writes:

> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>
>>> It's trivial to fix that problem: simply require implementations
>>> to define a preprocessor symbol about how the implementation
>>> works. Problem solved.
>>>
>>> (There are other instances of implementation-defined behavior
>>> that would benefit from analogous changes along these lines.)
>>
>> I tend to agree that such a preprocessor symbol would be an
>> improvement.
>>
>> I still think, as I wrote above, that removing the permission to
>> return a null pointer on a successful zero-sized allocation would
>> be a greater improvement.
>
> Fully agreed. That permission has been grit in the gears for a
> very long time, for much of which I had the misfortune of having
> to deal with it in real life thanks to IBM's bad decisions.
>
> Fixing it would have been, what, a 2-line change to the impacted
> implementations, [...]

Again you are assuming that one choice is good for everyone.
The people who wrote the C standard considered the question
at length and reached a different conclusion.

Incidentally, the change needed is a lot more than two lines.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<86h6j4fzo4.fsf@linuxsc.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49124&group=comp.lang.c#49124

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17687@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Mon, 22 Jan 2024 20:20:59 -0800
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <86h6j4fzo4.fsf@linuxsc.com>
References: <20240116162506.143@kylheku.com> <QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com> <9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com> <QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me> <20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com> <20240119134055.699@kylheku.com> <861qaaj3k9.fsf@linuxsc.com> <uolngi$nke1$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="c985c2e759848cd31734101b28e08031";
logging-data="1202644"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18brGbuKFktnX6Mqr16OolizV24z1uu0I0="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:+VFacRuuUG8K1wBGG2tux7Cq+e0=
sha1:t+Y/Cu5JibSsN1DayTpE/0t1T9E=
 by: Tim Rentsch - Tue, 23 Jan 2024 04:20 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:

> On 21/01/2024 12:04, Tim Rentsch wrote:
>
>> Kaz Kylheku <433-929-6894@kylheku.com> writes:
>>
>> [...]
>>
>>> Not requiring the non-null return from malloc(0) to be distinct
>>> from previous malloc(0) return values (whether they were freed or not),
>>> could help to "sell" the idea of taking away the null return value.
>>>
>>> Some implementors might grumble that null return allowed malloc(0) to be
>>> efficient by not allocating anything. If they were allowed to return
>>> (void *) -1 or something, that would placate that concern. [...]
>>
>> You have the tail wagging the dog here. If the results of
>> different malloc(0) calls don't need to be distinguishable,
>> they might just as well all be null.
>
> No, because it's natural to write
>
> employees = malloc(Nemployees * sizeof(EMPLOYEE));
> if (!employees)
> goto out_of_memory;
>
> You don't want to have to special case Nemployees == 0.

(A) You misunderstood the point I was making (as Keith's
followups more or less explained).

(B) Handling both implementation choices is trivial:
employees = malloc(Nemployees * sizeof(EMPLOYEE));
if (!employees && Nemployees)
goto out_of_memory;

(C) Alternatively, it is quite straightforward to write
a wrapper to malloc() and free() that returns a non-null
pointer for a size of zero (and that is a lower cost
path in terms of cycles used and memory resources needed
than the behavior implementations are required to provide
if they return a non-null pointer).

(D) The code you suggest is "natural" for people who are
lazy programmers who don't care about their code being
correct. There are easy ways around the deficiencies
of your example and any competent developer will use them.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uonlkk$15hqd$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49128&group=comp.lang.c#49128

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuyper@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Tue, 23 Jan 2024 01:16:52 -0500
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <uonlkk$15hqd$1@dont-email.me>
References: <20240116162506.143@kylheku.com>
<pan$eed53$d5a25673$c4eed852$f5f44c0@invalid.invalid>
<20240117160615.197@kylheku.com>
<pan$b42aa$a1ca5f31$3fabcad$7e6ed64a@invalid.invalid>
<20240118144637.733@kylheku.com>
<pan$a4d49$9d845f9f$d4df4789$9fb1b256@invalid.invalid>
<20240119083826.926@kylheku.com>
<pan$bb552$50ca4b2e$1541d619$e7097d14@invalid.invalid>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 23 Jan 2024 06:16:52 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="39c41b92fd2798faeea761dd61d5ee98";
logging-data="1230669"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cunvNefRNnpoWdUsFleVWYWZso9CxAl8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:H58b5RNvSoI8kMIHEp1ldvssD1Q=
Content-Language: en-US
In-Reply-To: <pan$bb552$50ca4b2e$1541d619$e7097d14@invalid.invalid>
 by: James Kuyper - Tue, 23 Jan 2024 06:16 UTC

On 1/22/24 18:12, Blue-Maned_Hawk wrote:
> Kaz Kylheku wrote:
>
>> On 2024-01-19, Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> wrote:
>>> Kaz Kylheku wrote:
>>>> Of course, that's not what "behavior can be readily inferred" means,
>>>> no matter where I got the example.
>>>
>>> Inferences may be makeäble, but they have no power.
>>
>> Yeah, tell that to your compiler when it surprisingly deletes code based
>> on inference.
>
>
> Apologies, let me clarify: In a technical standard, inferences from
> nonnormative text cannot be used to make normative statements.

True, but the relevant inferences were from normative text. The context
of the above comments has been snipped, so I'll repeat it here:

On 1/17/24 19:10, Kaz Kylheku wrote:
> On 2024-01-17, Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> wrote:
>> It looks to me like it was always undefined behavior, but it just wasn't
>> explicitly stated as such in C99.
>
> That is incorrect. A behavior for the following can be readily inferred
> from C99:
>
> void *p = malloc(42);

Per C99:
"The malloc function returns either a null pointer or a pointer to the
allocated space." (7.22.3.4p2)

Thus, there are two possibilities:
A: p == NULL
B: p contains the address of a block of memory at least 42 bytes long,
suitably aligned to store an object of any type.

The standard does not mandate documentation of which of the two applies,
so this has unspecified behavior.

> void *q = realloc(p, 0);

Per C99:
"The realloc function deallocates the old object pointed to by ptr and
returns a pointer to a new object that has the size specified by size.
The contents of the new object shall be the same as that of the old
object prior to deallocation, up to the lesser of the new and old sizes.
Any bytes in the new object beyond the size of the old object have
indeterminate values.

If ptr is a null pointer, the realloc function behaves like the malloc
function for the specified size. Otherwise, if ptr does not match a
pointer earlier returned by a memory management function, or if the
space has been deallocated by a call to the free or realloc function,
the behavior is undefined. If memory for the new object cannot be
allocated, the old object is not deallocated and its value is
unchanged." (7.22.3.5p2)

Thus, if situation A applies, then "p = realloc(p, 0)" is the equivalent
of p = malloc(0).

If situation B applies, then the memory pointed at by p is deallocated.
Since the value of p was in fact earlier returned by a memory management
function, the only statement that might otherwise give that statement
undefined behavior doesn't apply.

Either way, C99 says the following:
"If the size of the space requested is zero, the behavior is
implementation-defined: either a null pointer is returned, or the
behavior is as if the size were some nonzero value, except that the
returned pointer shall not be used to access an object." (7.22.3p1)

The behavior when the size is 0 is therefore implementation-defined.

Thus, situation A splits into two different possibilities:
A.a: q == NULL directly
A.b: q contains the same result as q = malloc(0), which splits into
A.b.a: q == NULL due to the equivalent of a call to malloc().
A.b.b: q points at a block of memory at least 1 byte of long, suitably
aligned to store an object of any type, but that pointer cannot be used
to access that memory (which renders the alignment effectively irrelevant).

A.a and A.b.a are indistinguishable, so there's really only two possible
outcomes for situation A.

Situation B is split the exact same way, except that B.b.b splits one
additional way: q might or might not end up containing a pointer to that
same memory that was allocated by the first call to malloc().

Thus, the behavior is a mixture of unspecified and
implementation-defined, giving several different possibilities, but none
of them has undefined behavior.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<uontao$16rlc$2@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49132&group=comp.lang.c#49132

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Tue, 23 Jan 2024 09:28:08 +0100
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <uontao$16rlc$2@dont-email.me>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<uoem42$39ohi$2@dont-email.me> <877ck5c8dl.fsf@nosuchdomain.example.com>
<20240119134055.699@kylheku.com> <uoj0nr$5qlf$1@dont-email.me>
<uojudd$bs5q$1@dont-email.me> <uojv5f$c17v$1@dont-email.me>
<uok0ah$c5dj$1@dont-email.me> <87il3mbcfy.fsf@nosuchdomain.example.com>
<uol8nr$l82b$2@dont-email.me> <uomp3e$tokp$5@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 23 Jan 2024 08:28:08 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c843dfd51cca0053a7acde203d8da68e";
logging-data="1273516"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18m54pXlLDOEwYM1yDpWBmHqrTdZ0goxic="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:iCXALsvLXL8XmosCUN7LpUcRQZQ=
In-Reply-To: <uomp3e$tokp$5@dont-email.me>
Content-Language: en-GB
 by: David Brown - Tue, 23 Jan 2024 08:28 UTC

On 22/01/2024 23:09, Lawrence D'Oliveiro wrote:
> On Mon, 22 Jan 2024 09:24:26 +0100, David Brown wrote:
>
>> On 21/01/2024 22:31, Keith Thompson wrote:
>>
>>> I suspect that calling malloc() with one size and free() with a
>>> different one would have been a rich source of subtle bugs.
>>>
>>>
>> Sure. But that's part of the fun of C :-)
>
> Not so much fun for the poor users who paid you the big bucks and expected
> to get working code in return.

Hence the smiley :-)

Re: *rubeyes*: realloc(ptr, 0) is UB?

<wwvcytsxuhg.fsf@LkoBDZeT.terraraq.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49134&group=comp.lang.c#49134

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!nntp.terraraq.uk!.POSTED.tunnel.sfere.anjou.terraraq.org.uk!not-for-mail
From: invalid@invalid.invalid (Richard Kettlewell)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Tue, 23 Jan 2024 09:35:39 +0000
Organization: terraraq NNTP server
Message-ID: <wwvcytsxuhg.fsf@LkoBDZeT.terraraq.uk>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com>
<87r0iech8j.fsf@nosuchdomain.example.com>
<20240118185544.347@kylheku.com> <865xzmj4bv.fsf@linuxsc.com>
<wwvh6j5mzxv.fsf@LkoBDZeT.terraraq.uk> <86plxsg0k9.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: innmantic.terraraq.uk; posting-host="tunnel.sfere.anjou.terraraq.org.uk:172.17.207.6";
logging-data="29576"; mail-complaints-to="usenet@innmantic.terraraq.uk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:iE4TM8OourvGYYTlslASLM41fFo=
X-Face: h[Hh-7npe<<b4/eW[]sat,I3O`t8A`(ej.H!F4\8|;ih)`7{@:A~/j1}gTt4e7-n*F?.Rl^
F<\{jehn7.KrO{!7=:(@J~]<.[{>v9!1<qZY,{EJxg6?Er4Y7Ng2\Ft>Z&W?r\c.!4DXH5PWpga"ha
+r0NzP?vnz:e/knOY)PI-
X-Boydie: NO
 by: Richard Kettlewell - Tue, 23 Jan 2024 09:35 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
> Richard Kettlewell <invalid@invalid.invalid> writes:
>> Of course that's what the cautious programmer must do practice. But in
>> terms of the total cost (over all users, implementers, etc) fixing the
>> definitions of memcpy/memset/etc (as well as malloc) would have been the
>> better answer.
>
> Better in some ways, worse in others. Better for me is not
> always the same as better for thee.

The way I think it’s better is total cost over all users, implementers,
etc. I think the time and effort spent coping with the bugs arising from
the current loose rules is much greater than the time it’d take to add
‘if(size == 0) { size=1; }’ to a handful of allocators and update some
unit tests.

>> Standard C is trying to have its own cake and eat it here: 0-sized
>> allocations can be represented by null pointers when it's malloc, but
>> not when it's memcpy.
>
> Actually the two decisions have essentially nothing to do with
> each other. You might want to read what the C Rationale
> document has to say about the decisions behind various
> memory allocation policies.

Do you mean
https://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf
s7.20.3?

--
https://www.greenend.org.uk/rjk/

Re: *rubeyes*: realloc(ptr, 0) is UB?

<868r4gf05a.fsf@linuxsc.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49140&group=comp.lang.c#49140

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17687@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Tue, 23 Jan 2024 09:08:17 -0800
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <868r4gf05a.fsf@linuxsc.com>
References: <20240116162506.143@kylheku.com> <QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com> <9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com> <QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me> <20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me> <20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com> <20240118185544.347@kylheku.com> <865xzmj4bv.fsf@linuxsc.com> <wwvh6j5mzxv.fsf@LkoBDZeT.terraraq.uk> <86plxsg0k9.fsf@linuxsc.com> <wwvcytsxuhg.fsf@LkoBDZeT.terraraq.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="c985c2e759848cd31734101b28e08031";
logging-data="1448807"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18QB6kasyXPIAFq0StaECTHDIW0MM3aXPQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:JgJQHqU4YJ83DbiVjsyfabb1fh0=
sha1:RKdT3YzZ4TmirE8mj+mVU3ujiTo=
 by: Tim Rentsch - Tue, 23 Jan 2024 17:08 UTC

Richard Kettlewell <invalid@invalid.invalid> writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Richard Kettlewell <invalid@invalid.invalid> writes:
>>
>>> Of course that's what the cautious programmer must do practice. But in
>>> terms of the total cost (over all users, implementers, etc) fixing the
>>> definitions of memcpy/memset/etc (as well as malloc) would have been the
>>> better answer.
>>
>> Better in some ways, worse in others. Better for me is not
>> always the same as better for thee.
>
> The way I think it's better is total cost over all users, implementers,
> etc. [...]

Yes, I got that. Not everyone shares that view. It's not
even a given that everyone thinks that's the best metric
to optimize.

>>> Standard C is trying to have its own cake and eat it here: 0-sized
>>> allocations can be represented by null pointers when it's malloc, but
>>> not when it's memcpy.
>>
>> Actually the two decisions have essentially nothing to do with
>> each other. You might want to read what the C Rationale
>> document has to say about the decisions behind various
>> memory allocation policies.
>
> Do you mean
> https://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf
> s7.20.3?

I was looking at C99RationaleV5.10, although I believe the comments
are from the earlier, original Rationale document. I expect you
have the right section; I don't have the document open to check.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<864jf3g735.fsf@linuxsc.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49147&group=comp.lang.c#49147

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!nntp.comgw.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17687@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Tue, 23 Jan 2024 11:53:02 -0800
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <864jf3g735.fsf@linuxsc.com>
References: <20240116162506.143@kylheku.com> <QdTpN.168167$vFZa.62153@fx13.iad> <86zfx3jtv9.fsf@linuxsc.com> <OdbqN.230721$c3Ea.136697@fx10.iad> <867ck4kgj8.fsf@linuxsc.com> <MqEqN.225660$7sbb.180523@fx16.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="c985c2e759848cd31734101b28e08031";
logging-data="1504346"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19EzHVxgVy2fMhD5waWOAdREuhZd4aHwXs="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:mwpdpWHLdkPK7diehGZMHv61XFY=
sha1:05XTyBw2n/jSDuzG0JGJ/6EnioE=
 by: Tim Rentsch - Tue, 23 Jan 2024 19:53 UTC

scott@slp53.sl.home (Scott Lurndal) writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> scott@slp53.sl.home (Scott Lurndal) writes:
>>
>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>
>>>> scott@slp53.sl.home (Scott Lurndal) writes:
>>>>
>>>>> Kaz Kylheku <433-929-6894@kylheku.com> writes:
>>>>>
>>>>>> I'm looking at the C99 and N3096 (April 2023) definitions of realloc
>>>>>> side by side.
>>>>>>
>>>>>> N3096 says
>>>>>>
>>>>>> "Otherwise, if ptr does not match a pointer earlier returned by a memory
>>>>>> management function, or if the space has been deallocated by a call to
>>>>>> the free or realloc function, or if the size is zero, the behavior is
>>>>>> undefined."
>>>>>>
>>>>>> Yikes! In C99 there is nothing about the size being zero:
>>>>>>
>>>>>> "Otherwise, if ptr does not match a pointer earlier returned by the
>>>>>> calloc, malloc, or realloc function, or if the space has been
>>>>>> deallocated by a call to the free or realloc function, the behavior is
>>>>>> undefined."
>>>>>>
>>>>>> Nothing about "or if the size is zero".
>>>>>>
>>>>>> Yikes; when did this criminal stupidity get perpetrated?
>>>>>
>>>>> I'm not sure what stupidity you are referring to, but IIRC, there was
>>>>> some recent standardization activity relating to realloc
>>>>> when size == 0 because there were differences in the behavior
>>>>> between different implementations. Making the behavior
>>>>> undefined was the only rational choice.
>>>>
>>>> Is that last sentence your own assessment, or are you simply
>>>> repeating someone else's assessment?
>>>
>>> My assessment. I've never found realloc useful, regardless of
>>> the value of the size parameter. YMMV.
>>
>> So you aren't really in a position to say whether this decision was
>> the only rational choice. Generally I hope people who would make
>> such a statement would first make an effort to learn and understand
>> other people's thoughts on the matter.
>
> The fact that I didn't find it useful, doesn't imply in any way
> that I don't understand other's thoughts on the matter. I did
> spend several years on various standards committees, compromise
> was often the best way to make forward progress and 'undefined
> behavior' was a rational compromise in that context.

There's a big difference between saying something is a rational
compromise and saying it's the only rational choice. And you
still haven't provided any evidence that you are in a position to
offer an informed judgment on either premise, for the particular
question of how realloc(p,0) should be treated. If all you're
expressing is a personal opinion, I have no problem with that.
If you want to claim that you are offering more than just a
personal opinion, I think it's reasonable to ask for some
supporting evidence to back that up.

Re: *rubeyes*: realloc(ptr, 0) is UB?

<20240123135221.827@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=49162&group=comp.lang.c#49162

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!newsfeed.endofthelinebbs.com!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: *rubeyes*: realloc(ptr, 0) is UB?
Date: Tue, 23 Jan 2024 21:56:18 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <20240123135221.827@kylheku.com>
References: <20240116162506.143@kylheku.com>
<QdTpN.168167$vFZa.62153@fx13.iad> <20240117094759.508@kylheku.com>
<9iYpN.354613$83n7.275953@fx18.iad> <86r0ifjbiw.fsf@linuxsc.com>
<QfbqN.230722$c3Ea.54531@fx10.iad> <uobqhg$2mt8c$1@dont-email.me>
<20240118112920.465@kylheku.com> <uoc030$2ndid$2@dont-email.me>
<20240118144021.3@kylheku.com> <87r0iech8j.fsf@nosuchdomain.example.com>
<20240118185544.347@kylheku.com> <865xzmj4bv.fsf@linuxsc.com>
<wwvh6j5mzxv.fsf@LkoBDZeT.terraraq.uk> <86plxsg0k9.fsf@linuxsc.com>
<wwvcytsxuhg.fsf@LkoBDZeT.terraraq.uk> <868r4gf05a.fsf@linuxsc.com>
Injection-Date: Tue, 23 Jan 2024 21:56:18 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="89a24c451ef1c6e54f4783ce54b57f43";
logging-data="1536287"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+KXmW9DsFuQIxBP7stmmulDReHl73qW/k="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:mvymqfilV+H+0gPbveoqgGa/Jko=
 by: Kaz Kylheku - Tue, 23 Jan 2024 21:56 UTC

On 2024-01-23, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> Richard Kettlewell <invalid@invalid.invalid> writes:
>
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>
>>> Richard Kettlewell <invalid@invalid.invalid> writes:
>>>
>>>> Of course that's what the cautious programmer must do practice. But in
>>>> terms of the total cost (over all users, implementers, etc) fixing the
>>>> definitions of memcpy/memset/etc (as well as malloc) would have been the
>>>> better answer.
>>>
>>> Better in some ways, worse in others. Better for me is not
>>> always the same as better for thee.
>>
>> The way I think it's better is total cost over all users, implementers,
>> etc. [...]
>
> Yes, I got that. Not everyone shares that view. It's not
> even a given that everyone thinks that's the best metric
> to optimize.

It is an inherently collectivist view; I would expect communists
to agree with it unanimously. (But then bungle the execution by
forming a five year plan in which the work is divided into groups
driven by meaningless local metrics and irrelevant incentives.)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.


devel / comp.lang.c / Re: *rubeyes*: realloc(ptr, 0) is UB?

Pages:1234567
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor