Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

E = MC ** 2 +- 3db


devel / comp.lang.c / Re: The return value of realloc(p,0)

SubjectAuthor
* The return value of realloc(p,0)Kenny McCormack
+* The return value of realloc(p,0)Richard Damon
|+* The return value of realloc(p,0)Robert Latest
||`* The return value of realloc(p,0)Tim Rentsch
|| `* The return value of realloc(p,0)Vir Campestris
||  `* The return value of realloc(p,0)Tim Rentsch
||   +* The return value of realloc(p,0)Vir Campestris
||   |+- The return value of realloc(p,0)Kaz Kylheku
||   |`* The return value of realloc(p,0)Tim Rentsch
||   | +* The return value of realloc(p,0)Scott Lurndal
||   | |`* The return value of realloc(p,0)Tim Rentsch
||   | | `* The return value of realloc(p,0)Scott Lurndal
||   | |  `* The return value of realloc(p,0)Tim Rentsch
||   | |   `* The return value of realloc(p,0)Scott Lurndal
||   | |    +* The return value of realloc(p,0)Spiros Bousbouras
||   | |    |`- The return value of realloc(p,0)Malcolm McLean
||   | |    +* The return value of realloc(p,0)Tim Rentsch
||   | |    |`* The return value of realloc(p,0)Scott Lurndal
||   | |    | `- The return value of realloc(p,0)Tim Rentsch
||   | |    `- The return value of realloc(p,0)Vir Campestris
||   | `- The return value of realloc(p,0)Vir Campestris
||   `* The return value of realloc(p,0)Kaz Kylheku
||    +* The return value of realloc(p,0)Lew Pitcher
||    |+* The return value of realloc(p,0)Kaz Kylheku
||    ||`- The return value of realloc(p,0)Lew Pitcher
||    |`* The return value of realloc(p,0)Tim Rentsch
||    | `* The return value of realloc(p,0)Kaz Kylheku
||    |  `- The return value of realloc(p,0)Tim Rentsch
||    `- The return value of realloc(p,0)Tim Rentsch
|`* The return value of realloc(p,0)Tim Rentsch
| `- The return value of realloc(p,0)Scott Lurndal
+* The return value of realloc(p,0)Chris M. Thomasson
|`* The return value of realloc(p,0)Malcolm McLean
| `* The return value of realloc(p,0)Ben Bacarisse
|  `* The return value of realloc(p,0)Chris M. Thomasson
|   `- The return value of realloc(p,0)Chris M. Thomasson
`* The return value of realloc(p,0)Tim Rentsch
 `- The return value of realloc(p,0)Scott Lurndal

Pages:12
Re: The return value of realloc(p,0)

<kl972jF24chU3@mid.individual.net>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: boblatest@yahoo.com (Robert Latest)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: 30 Aug 2023 15:54:59 GMT
Lines: 8
Message-ID: <kl972jF24chU3@mid.individual.net>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad>
X-Trace: individual.net khD6qKh326EiN3P4tYN+Awl8VHQS+paLTd+hXHDRRbGf3O93Jk
Cancel-Lock: sha1:B6L8X2O6J9Rhj+ny490HBy5Co1o= sha256:wZfgqJyjzsJ2fzfeDKNmQLLJgmUn0boaySvmHHe2yRc=
User-Agent: slrn/1.0.3 (Linux)
 by: Robert Latest - Wed, 30 Aug 2023 15:54 UTC

Richard Damon wrote:
> My understanding is that, in effect, the call to realloc(p,0), if it is
> using the second option of returning a suitable pointer, is effectively
> do a free(p), and then allocating a "0 byte" block of memory and
> returning the address of that.

Or it just returns p, as it might do whenever the requested new size is smaller
than the already-allocated size. No reason for 0 to be a special case.

Re: The return value of realloc(p,0)

<86edjju668.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Wed, 30 Aug 2023 22:33:51 -0700
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <86edjju668.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="49a0c7fba7d7c0f06cea865d80b29294";
logging-data="3295808"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/lkgkhs4+CbtRaynZ5wV26G+QiVQgjw3g="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:GRqX1QNG2qjfvT7yYfDqw0u92ck=
sha1:QG5rLDzdeGRdDRxmzUJ+wm7tRCI=
 by: Tim Rentsch - Thu, 31 Aug 2023 05:33 UTC

Robert Latest <boblatest@yahoo.com> writes:

> Richard Damon wrote:
>
>> My understanding is that, in effect, the call to realloc(p,0), if
>> it is using the second option of returning a suitable pointer, is
>> effectively do a free(p), and then allocating a "0 byte" block of
>> memory and returning the address of that.
>
> Or it just returns p, as it might do whenever the requested new size
> is smaller than the already-allocated size. No reason for 0 to be a
> special case.

There are reasons for realloc() to zize 0 to be a special case.
Specifically, because the pointer value returned doesn't have to
be dereferencable, it can point to parts of the address space
that are otherwise unusable. The overhead for such "zero-byte
blocks" is very low, just a smidge over one bit. The usuable
memory previously allocated can be reclaimed and used in its
entirely for subsequent allocations. If running in a 64-bit
address space, vast amounts of otherwise unusable areas can
be used for these "special objects", having very little impact
on memory usable for actual objects.

Re: The return value of realloc(p,0)

<ucqrn0$3dvle$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vir.campestris@invalid.invalid (Vir Campestris)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Thu, 31 Aug 2023 21:03:44 +0100
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <ucqrn0$3dvle$1@dont-email.me>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 31 Aug 2023 20:03:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="64012ee335ecafeb8d167eb95553e90e";
logging-data="3604142"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bEIudIdkeR7FOHreFE5mpPTigjgVkVZg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:XZjhAutCKV7mf4axc+Zqig8vMD4=
Content-Language: en-GB
In-Reply-To: <86edjju668.fsf@linuxsc.com>
 by: Vir Campestris - Thu, 31 Aug 2023 20:03 UTC

On 31/08/2023 06:33, Tim Rentsch wrote:
> There are reasons for realloc() to zize 0 to be a special case.
> Specifically, because the pointer value returned doesn't have to
> be dereferencable, it can point to parts of the address space
> that are otherwise unusable. The overhead for such "zero-byte
> blocks" is very low, just a smidge over one bit. The usuable
> memory previously allocated can be reclaimed and used in its
> entirely for subsequent allocations. If running in a 64-bit
> address space, vast amounts of otherwise unusable areas can
> be used for these "special objects", having very little impact
> on memory usable for actual objects.

Tim, I'm confused by a little bit of that:

"just a smidge over one bit"

I can see how you might point them all to a single place that can't be
read or written, and have safety in case anyone tries to use them.

I can see that you might have a section of memory with a series of
bytes, each one reserved for these empty allocations. It might be
inaccessible, but will still use address space.

But how do you get anything smaller than a byte, but non-zero? I've only
ever used one CPU with addressing smaller than a byte.

Andy

Re: The return value of realloc(p,0)

<86wmxas6ty.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Fri, 01 Sep 2023 00:14:49 -0700
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <86wmxas6ty.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="e13a49a448cc252f76540c1a5198a3b9";
logging-data="3881751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/4P3BVM+AepN3c8jdRIo1lkuz5xJ/xtf4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:NLQeEX2ldIPinbtq1LvwcE3M8H8=
sha1:64VsRHK8T0w0gSvPLpPX/NldY6E=
 by: Tim Rentsch - Fri, 1 Sep 2023 07:14 UTC

Vir Campestris <vir.campestris@invalid.invalid> writes:

> On 31/08/2023 06:33, Tim Rentsch wrote:
>
>> There are reasons for realloc() to zize 0 to be a special case.
>> Specifically, because the pointer value returned doesn't have to
>> be dereferencable, it can point to parts of the address space
>> that are otherwise unusable. The overhead for such "zero-byte
>> blocks" is very low, just a smidge over one bit. The usuable
>> memory previously allocated can be reclaimed and used in its
>> entirely for subsequent allocations. If running in a 64-bit
>> address space, vast amounts of otherwise unusable areas can
>> be used for these "special objects", having very little impact
>> on memory usable for actual objects.
>
> Tim, I'm confused by a little bit of that:
>
> "just a smidge over one bit"
>
> I can see how you might point them all to a single place that can't be
> read or written, and have safety in case anyone tries to use them.
>
> I can see that you might have a section of memory with a series of
> bytes, each one reserved for these empty allocations. It might be
> inaccessible, but will still use address space.
>
> But how do you get anything smaller than a byte, but non-zero? I've
> only ever used one CPU with addressing smaller than a byte.

I was tempted to make a joke here, but I couldn't see how to turn
out something funny.

What I meant was just over one bit /per zero-byte block/. So if
there were 20,000 zero-byte blocks, they could be managed with
just a little more than 2500 bytes of allocated memory. Sorry
for the confusion.

Re: The return value of realloc(p,0)

<ucsejp$3nrkk$5@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vir.campestris@invalid.invalid (Vir Campestris)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Fri, 1 Sep 2023 11:32:25 +0100
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <ucsejp$3nrkk$5@dont-email.me>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me>
<86wmxas6ty.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 1 Sep 2023 10:32:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7263a072cd3654f0dd3422ffc68d0f2d";
logging-data="3927700"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Co2CxF4qLCL/RYiA9QVthw7mQkbhJt6c="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:qhzYdoMU7sWdNUkKrVWfbOwViRA=
In-Reply-To: <86wmxas6ty.fsf@linuxsc.com>
Content-Language: en-GB
 by: Vir Campestris - Fri, 1 Sep 2023 10:32 UTC

On 01/09/2023 08:14, Tim Rentsch wrote:
> Vir Campestris <vir.campestris@invalid.invalid> writes:
>
>> On 31/08/2023 06:33, Tim Rentsch wrote:
>>
>>> There are reasons for realloc() to zize 0 to be a special case.
>>> Specifically, because the pointer value returned doesn't have to
>>> be dereferencable, it can point to parts of the address space
>>> that are otherwise unusable. The overhead for such "zero-byte
>>> blocks" is very low, just a smidge over one bit. The usuable
>>> memory previously allocated can be reclaimed and used in its
>>> entirely for subsequent allocations. If running in a 64-bit
>>> address space, vast amounts of otherwise unusable areas can
>>> be used for these "special objects", having very little impact
>>> on memory usable for actual objects.
>>
>> Tim, I'm confused by a little bit of that:
>>
>> "just a smidge over one bit"
>>
>> I can see how you might point them all to a single place that can't be
>> read or written, and have safety in case anyone tries to use them.
>>
>> I can see that you might have a section of memory with a series of
>> bytes, each one reserved for these empty allocations. It might be
>> inaccessible, but will still use address space.
>>
>> But how do you get anything smaller than a byte, but non-zero? I've
>> only ever used one CPU with addressing smaller than a byte.
>
> I was tempted to make a joke here, but I couldn't see how to turn
> out something funny.
>
> What I meant was just over one bit /per zero-byte block/. So if
> there were 20,000 zero-byte blocks, they could be managed with
> just a little more than 2500 bytes of allocated memory. Sorry
> for the confusion.

I still don't follow you.

If I realloc to size 0 I get an address back. That can be a unique
address, in which case each one uses an address, and therefore a byte.

It can be a shared address, in which case the overhead per allocation is 0.

When I call free it would look at the shared address, and do nothing; if
it's a non-shared address it moves it to a free pool.

What does your bit do?

Andy

Re: The return value of realloc(p,0)

<20230901095453.20@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Fri, 1 Sep 2023 17:03:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <20230901095453.20@kylheku.com>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me>
<86wmxas6ty.fsf@linuxsc.com>
Injection-Date: Fri, 1 Sep 2023 17:03:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d7d6043c57cccad85a622dc3d680f17e";
logging-data="4141537"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+tePOFON8/1M/9aiRpq2nY4lwYrI7z2UU="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:A8PLWy4KenHaZHdpYEtit/M+rA8=
 by: Kaz Kylheku - Fri, 1 Sep 2023 17:03 UTC

On 2023-09-01, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> What I meant was just over one bit /per zero-byte block/. So if
> there were 20,000 zero-byte blocks, they could be managed with
> just a little more than 2500 bytes of allocated memory. Sorry
> for the confusion.

The following is a much better requirement for malloc and realloc:

In the zero size case, return a non-null pointer to a single
program-wide object (always the same one), which can be passed
to the free function any number of times.

Since a zero sized object doesn't store any information, there
is no need to have more than one.

We satisfy these requirements:

- realloc(x, 0) is effectively free(x), except that it also
returns that object.

- the non-null object can be passed to library functions whose
behavior is not defined for null pointers, provided the
functions are not instructed to dereference it, or perform
pointer arithmetic.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: The return value of realloc(p,0)

<20230901100330.31@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Fri, 1 Sep 2023 17:08:01 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <20230901100330.31@kylheku.com>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me>
<86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me>
Injection-Date: Fri, 1 Sep 2023 17:08:01 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d7d6043c57cccad85a622dc3d680f17e";
logging-data="4141537"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18n1m7kEJiBETStRTxtpi0E5heBEDT6bS4="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:Xru2dmWaScuoitUmcl+Q0RXdciA=
 by: Kaz Kylheku - Fri, 1 Sep 2023 17:08 UTC

On 2023-09-01, Vir Campestris <vir.campestris@invalid.invalid> wrote:
> It can be a shared address, in which case the overhead per allocation is 0.
>
> When I call free it would look at the shared address, and do nothing; if
> it's a non-shared address it moves it to a free pool.
>
> What does your bit do?

The address returned has to be a unique byte, but it doesn't have to
point to valid virtual or physical memory. On, say, 64 bit systems,
there is a lot of pointer space to play with that will never be used
for storage.

But you have to track, "did we allocate that pointer or not",
which is a Boolean property. That can be packed into a bit array
somewhere, whose elements correspond to a consecutive range of
some pointers. Just like a free sector map in MS-DOS FAT or whatever.

It's a decent implementation of the (possible) protocol that we have in
the standard of allocating unique objects for zero-sized requests.

The protocol itself is a silly idea, though.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: The return value of realloc(p,0)

<uct8mu$3pgff$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Fri, 1 Sep 2023 17:57:50 -0000 (UTC)
Organization: The Pitcher Digital Freehold
Lines: 40
Message-ID: <uct8mu$3pgff$1@dont-email.me>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me>
<86wmxas6ty.fsf@linuxsc.com> <20230901095453.20@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 1 Sep 2023 17:57:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="54dd6ff3c5151f08421ab5417076d737";
logging-data="3981807"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/L4y9gKWzElnbIbFG2YUfyCb409cgNWhY="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:McEhL9P/vpctXKxrfHZIpO+wmgc=
 by: Lew Pitcher - Fri, 1 Sep 2023 17:57 UTC

On Fri, 01 Sep 2023 17:03:06 +0000, Kaz Kylheku wrote:

> On 2023-09-01, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>> What I meant was just over one bit /per zero-byte block/. So if there
>> were 20,000 zero-byte blocks, they could be managed with just a little
>> more than 2500 bytes of allocated memory. Sorry for the confusion.
>
> The following is a much better requirement for malloc and realloc:
>
> In the zero size case, return a non-null pointer to a single
> program-wide object (always the same one), which can be passed to the
> free function any number of times.
>
> Since a zero sized object doesn't store any information, there is no
> need to have more than one.
>
> We satisfy these requirements:
>
> - realloc(x, 0) is effectively free(x), except that it also
> returns that object.
>
> - the non-null object can be passed to library functions whose
> behavior is not defined for null pointers, provided the functions are
> not instructed to dereference it, or perform pointer arithmetic.

This last point implies that there must be some way for code to
distinguish this non-null object (that cannot be dereferenced)
from other non-null objects that /can/ be dereferenced.

Given that you specify that the non-null pointer returned from
realloc(x,0)
would be "a single program-wide object" and "always the same one",
would you consider providing a unique name (like we do for the
null pointer constant NULL) for such an object? If so, what name
would you suggest, and what would the definition look like?

Just curious,
--
Lew Pitcher
"In Skills We Trust"

Re: The return value of realloc(p,0)

<86sf7xspk3.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Fri, 01 Sep 2023 11:42:36 -0700
Organization: A noiseless patient Spider
Lines: 78
Message-ID: <86sf7xspk3.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="e13a49a448cc252f76540c1a5198a3b9";
logging-data="4175704"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zokdnKEnWVmvU6tZy+dy7c2pObwsMivA="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:qVwPFGr8Kz4eK/WDdEScCkcDHA0=
sha1:8tTVocXtpMtb2ZF60t68spNMgPw=
 by: Tim Rentsch - Fri, 1 Sep 2023 18:42 UTC

Vir Campestris <vir.campestris@invalid.invalid> writes:

> On 01/09/2023 08:14, Tim Rentsch wrote:
>
>> Vir Campestris <vir.campestris@invalid.invalid> writes:
>>
>>> On 31/08/2023 06:33, Tim Rentsch wrote:
>>>
>>>> There are reasons for realloc() to zize 0 to be a special case.
>>>> Specifically, because the pointer value returned doesn't have to
>>>> be dereferencable, it can point to parts of the address space
>>>> that are otherwise unusable. The overhead for such "zero-byte
>>>> blocks" is very low, just a smidge over one bit. The usuable
>>>> memory previously allocated can be reclaimed and used in its
>>>> entirely for subsequent allocations. If running in a 64-bit
>>>> address space, vast amounts of otherwise unusable areas can
>>>> be used for these "special objects", having very little impact
>>>> on memory usable for actual objects.
>>>
>>> Tim, I'm confused by a little bit of that:
>>>
>>> "just a smidge over one bit"
>>>
>>> I can see how you might point them all to a single place that can't be
>>> read or written, and have safety in case anyone tries to use them.
>>>
>>> I can see that you might have a section of memory with a series of
>>> bytes, each one reserved for these empty allocations. It might be
>>> inaccessible, but will still use address space.
>>>
>>> But how do you get anything smaller than a byte, but non-zero? I've
>>> only ever used one CPU with addressing smaller than a byte.
>>
>> I was tempted to make a joke here, but I couldn't see how to turn
>> out something funny.
>>
>> What I meant was just over one bit /per zero-byte block/. So if
>> there were 20,000 zero-byte blocks, they could be managed with
>> just a little more than 2500 bytes of allocated memory. Sorry
>> for the confusion.
>
> I still don't follow you.
>
> If I realloc to size 0 I get an address back. That can be a unique
> address, in which case each one uses an address, and therefore a byte.
> [...]

In fact it must be a unique address, distinct from any other
address that has been allocated but not yet freed.

The key point is that, when the size is zero, the allocation
routine allocates /address space/ but does not allocate /memory/.
The address is not mapped to any memory, either physical or
virtual.

In a 64-bit address space, there are a lot more addresses than
there are bytes of memory, even virtual bytes of memory. All but
a very small fraction of 64-bit addresses cannot map to actual
memory, because there is no room on the address bus (figuratively
speaking).

For example, suppose our zero byte block reside at addresses
in the range

0x800000000000 to
0x802000000000

and "objects" are aligned to 32-byte boundaries. That means
there are 4294967296 zero-byte "objects" available. For each of
those objects, we need to know if it has been allocated by not
yet freed, that is, one bit per object. Add in a few extra
pointers and counts to manage the space to hold the bitmap
(so we don't have to use much more space for the bitmap than
is necessary), and we get a little over one bit /of actual
memory/ that is used to manage the zero-byte objects, which
take no actual memory.

Does that make more sense now?

Re: The return value of realloc(p,0)

<20230901113229.84@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Fri, 1 Sep 2023 18:44:55 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 98
Message-ID: <20230901113229.84@kylheku.com>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me>
<86wmxas6ty.fsf@linuxsc.com> <20230901095453.20@kylheku.com>
<uct8mu$3pgff$1@dont-email.me>
Injection-Date: Fri, 1 Sep 2023 18:44:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d7d6043c57cccad85a622dc3d680f17e";
logging-data="4176411"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+xvGdstWWuN5D6jlYCudkYFCGTCVpv7cI="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:yf+HE/hRZoAFTkA+Yt8oFYZBF5I=
 by: Kaz Kylheku - Fri, 1 Sep 2023 18:44 UTC

On 2023-09-01, Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
> On Fri, 01 Sep 2023 17:03:06 +0000, Kaz Kylheku wrote:
>
>> On 2023-09-01, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>> What I meant was just over one bit /per zero-byte block/. So if there
>>> were 20,000 zero-byte blocks, they could be managed with just a little
>>> more than 2500 bytes of allocated memory. Sorry for the confusion.
>>
>> The following is a much better requirement for malloc and realloc:
>>
>> In the zero size case, return a non-null pointer to a single
>> program-wide object (always the same one), which can be passed to the
>> free function any number of times.
>>
>> Since a zero sized object doesn't store any information, there is no
>> need to have more than one.
>>
>> We satisfy these requirements:
>>
>> - realloc(x, 0) is effectively free(x), except that it also
>> returns that object.
>>
>> - the non-null object can be passed to library functions whose
>> behavior is not defined for null pointers, provided the functions are
>> not instructed to dereference it, or perform pointer arithmetic.
>
> This last point implies that there must be some way for code to
> distinguish this non-null object (that cannot be dereferenced)
> from other non-null objects that /can/ be dereferenced.

No, it doesn't.

Say we make this pointer:

char x[4], *p = x + 4;

f(p);

p effectively points to the base of a zero length array: one
byte past its last element.

Does f need "some what to distinguish this p value from one that
can bedereferenced"?
>
> Given that you specify that the non-null pointer returned from
> realloc(x,0)
> would be "a single program-wide object" and "always the same one",
> would you consider providing a unique name (like we do for the
> null pointer constant NULL) for such an object?

Tentatively yes. POSIX systems could use the same value as MAP_FAILED
which is typically

#define MAP_FAILED ((void *) -1)

It is sometimes useful to have an additional pointer value that is
not the same as any object, and invalid.

One example is in this source file:

https://www.kylheku.com/cgit/txr/tree/regex.c

Look for casts like coerce(cset_L2_t *, -1). I didn't define
a constant for this.

This code implements a multi-level radix tree structure for
representing Unicode character sets.

A null pointer at any level in the tree indicates that that
section of the space is all zero bits: those code points
under that tree are not in the set. We don't have to allocate
the underying directories and pages just to fill them wit zeros.

I also wanted an efficient way to say that an entire subtree
contains nothing but 1 bits. For that, I used a -1 converted
to the pointer type.

> If so, what name
> would you suggest, and what would the definition look like?

The definition cannot be dictated at the ISO C level;
only its properties can be described.

In some implementation ((void *) -1) might be indistinguishable
from the null pointer; you know the deal.

I'm not conviced that the constant would have to be published just for
the purpose of its connection with malloc(0); it would have to be
considered separately useful.

And even then, it would ahve to be decided whether that constant should
actually be that malloc(0) value, or should that still use its own
value.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: The return value of realloc(p,0)

<86fs3xsoyq.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Fri, 01 Sep 2023 11:55:25 -0700
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <86fs3xsoyq.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <20230901095453.20@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="e13a49a448cc252f76540c1a5198a3b9";
logging-data="4175704"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/PimG4WLYpJPUkt7FmAG5eL4NAVneYsRA="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:+P6KHiFINnu/zLvc9/O3LZI0ipE=
sha1:SYI/Chc11ybKVh8mrBe2mWrv4pQ=
 by: Tim Rentsch - Fri, 1 Sep 2023 18:55 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:

> On 2023-09-01, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
>> What I meant was just over one bit /per zero-byte block/. So if
>> there were 20,000 zero-byte blocks, they could be managed with
>> just a little more than 2500 bytes of allocated memory. Sorry
>> for the confusion.
>
> The following is a much better requirement for malloc and realloc:
>
> In the zero size case, return a non-null pointer to a single
> program-wide object (always the same one), which can be passed
> to the free function any number of times. [...]

This idea is worse in one important respect: it doesn't satisfy
the specification in the C standard.

Incidentally, it also doesn't satisfy the requirements of the
people who proposed the idea to begin with.

Re: The return value of realloc(p,0)

<86bkelsou0.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Fri, 01 Sep 2023 11:58:15 -0700
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <86bkelsou0.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <20230901095453.20@kylheku.com> <uct8mu$3pgff$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="e13a49a448cc252f76540c1a5198a3b9";
logging-data="4175704"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uIUZ980b6N7wGGdKNQRa68eCDefBaubg="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:q1Pbz7U7qgO4O2ZECFQSuRxjMIs=
sha1:0y87pZJm0ryR0dFGyXmyKLHh2qg=
 by: Tim Rentsch - Fri, 1 Sep 2023 18:58 UTC

Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:

> On Fri, 01 Sep 2023 17:03:06 +0000, Kaz Kylheku wrote:
>
>> On 2023-09-01, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>
>>> What I meant was just over one bit /per zero-byte block/. So if there
>>> were 20,000 zero-byte blocks, they could be managed with just a little
>>> more than 2500 bytes of allocated memory. Sorry for the confusion.
>>
>> The following is a much better requirement for malloc and realloc:
>>
>> In the zero size case, return a non-null pointer to a single
>> program-wide object (always the same one), which can be passed to the
>> free function any number of times.
>>
>> Since a zero sized object doesn't store any information, there is no
>> need to have more than one.
>>
>> We satisfy these requirements:
>>
>> - realloc(x, 0) is effectively free(x), except that it also
>> returns that object.
>>
>> - the non-null object can be passed to library functions whose
>> behavior is not defined for null pointers, provided the functions are
>> not instructed to dereference it, or perform pointer arithmetic.
>
> This last point implies that there must be some way for code to
> distinguish this non-null object (that cannot be dereferenced)
> from other non-null objects that /can/ be dereferenced.

The idea that zero-byte objects can all be the same doesn't
satisfy the specifications in the C standard.

Re: The return value of realloc(p,0)

<JCqIM.79177$m8Ke.22576@fx08.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx08.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: The return value of realloc(p,0)
Newsgroups: comp.lang.c
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me> <86sf7xspk3.fsf@linuxsc.com>
Lines: 25
Message-ID: <JCqIM.79177$m8Ke.22576@fx08.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 01 Sep 2023 19:06:17 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 01 Sep 2023 19:06:17 GMT
X-Received-Bytes: 1816
 by: Scott Lurndal - Fri, 1 Sep 2023 19:06 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>Vir Campestris <vir.campestris@invalid.invalid> writes:
>
>
>In a 64-bit address space, there are a lot more addresses than
>there are bytes of memory, even virtual bytes of memory. All but
>a very small fraction of 64-bit addresses cannot map to actual
>memory, because there is no room on the address bus (figuratively
>speaking).

Current processors support between 44 and 52 bits of physical
address, and for the most part, the same on the virtual address
space as well. ARMv8 allows the VA space to be anywhere
from 16 bits to 52 bits on a per-translation-context) basis;
where smaller VA sizes can significantly decrease translation
table walk cost (i.e. by skipping levels).

With DDR5 and HBM we're seeing terabytes of DRAM on the bus.

With CXL (and Intel's PUMA chip) the physical address bus
will be fully utilized (all 64 bits) in the not so distant
future, if perhaps sparsely by using higher order bits to
identify nodes on the fabric.

Re: The return value of realloc(p,0)

<20230901120930.698@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Fri, 1 Sep 2023 19:14:28 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <20230901120930.698@kylheku.com>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me>
<86wmxas6ty.fsf@linuxsc.com> <20230901095453.20@kylheku.com>
<uct8mu$3pgff$1@dont-email.me> <86bkelsou0.fsf@linuxsc.com>
Injection-Date: Fri, 1 Sep 2023 19:14:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d7d6043c57cccad85a622dc3d680f17e";
logging-data="4187119"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18c8O3HYTkiJnud4peV7xA6tsMITm22q5o="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:zob/e9MrwXc+3nx7cAw9KQsRSpM=
 by: Kaz Kylheku - Fri, 1 Sep 2023 19:14 UTC

On 2023-09-01, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> The idea that zero-byte objects can all be the same doesn't
> satisfy the specifications in the C standard.

yes; we all know are talking about an alternative requiremnt
that is not one of the current two implementation-defined choices.

However, anyone can implement the idea (or others) in an allocator
wrapper.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: The return value of realloc(p,0)

<uctham$3pgff$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Fri, 1 Sep 2023 20:24:54 -0000 (UTC)
Organization: The Pitcher Digital Freehold
Lines: 72
Message-ID: <uctham$3pgff$2@dont-email.me>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me>
<86wmxas6ty.fsf@linuxsc.com> <20230901095453.20@kylheku.com>
<uct8mu$3pgff$1@dont-email.me> <20230901113229.84@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 1 Sep 2023 20:24:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="54dd6ff3c5151f08421ab5417076d737";
logging-data="3981807"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195DMCw1mtNkBHbgcFz60mPNkEViEq0h4Y="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:ePkD7xoqYlw6Li+ZP0aHgINCAWE=
 by: Lew Pitcher - Fri, 1 Sep 2023 20:24 UTC

On Fri, 01 Sep 2023 18:44:55 +0000, Kaz Kylheku wrote:

> On 2023-09-01, Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
>> On Fri, 01 Sep 2023 17:03:06 +0000, Kaz Kylheku wrote:
>>
>>> On 2023-09-01, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>>> What I meant was just over one bit /per zero-byte block/. So if
>>>> there were 20,000 zero-byte blocks, they could be managed with just a
>>>> little more than 2500 bytes of allocated memory. Sorry for the
>>>> confusion.
>>>
>>> The following is a much better requirement for malloc and realloc:
>>>
>>> In the zero size case, return a non-null pointer to a single
>>> program-wide object (always the same one), which can be passed to the
>>> free function any number of times.
>>>
>>> Since a zero sized object doesn't store any information, there is no
>>> need to have more than one.
>>>
>>> We satisfy these requirements:
>>>
>>> - realloc(x, 0) is effectively free(x), except that it also
>>> returns that object.
>>>
>>> - the non-null object can be passed to library functions whose
>>> behavior is not defined for null pointers, provided the functions
>>> are not instructed to dereference it, or perform pointer arithmetic.
>>
>> This last point implies that there must be some way for code to
>> distinguish this non-null object (that cannot be dereferenced) from
>> other non-null objects that /can/ be dereferenced.
>
> No, it doesn't.
>
> Say we make this pointer:
>
> char x[4], *p = x + 4;
>
> f(p);
>
> p effectively points to the base of a zero length array: one byte past
> its last element.
>
> Does f need "some what to distinguish this p value from one that can
> be dereferenced"?

OK, I get that. I took your statement that "the non-null object can be
passed to library functions whose behavior is not defined for null
pointers, provided the functions are not instructed to dereference it,
or perform pointer arithmetic" as implying that, for instance
strcpy(realloc(x,0),"abc"); /* unrealistic, but... */
would need some way to determine that the function would need to check
the given destination pointer for it's zero-length not-nullness before
performing the requested copy operation. If it does not, then the
standard function would violate your "cannot be dereferenced or be the
subject of pointer arithmetic" rule.

So, I guess that strcpy() will just have to "take it on faith" that
the given pointer points to something of more than zero size.

ISTM that strcpy() can easily test it's arguments for NULLness and
cleanly abort the copy operation if either equals NULL, although it is
under no obligation to do so by the C standard. I wondered how the
function could ensure that neither of it's arguments were of the
not-null zero-length variety.

Thanks for the clarification.
--
Lew Pitcher
"In Skills We Trust"

Re: The return value of realloc(p,0)

<ud06jv$hibn$4@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vir.campestris@invalid.invalid (Vir Campestris)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Sat, 2 Sep 2023 21:40:30 +0100
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <ud06jv$hibn$4@dont-email.me>
References: <ua63if$2u50c$2@news.xmission.com>
<DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me>
<86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me>
<86sf7xspk3.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 2 Sep 2023 20:40:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="71f880443bfc728e6c9e8e2ede404518";
logging-data="575863"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18xORxTJjDGFFpavsdjAHqb/G6M03fN32g="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:Xrm+NGe+yx2CIAWq+quT0uz+YQ0=
Content-Language: en-GB
In-Reply-To: <86sf7xspk3.fsf@linuxsc.com>
 by: Vir Campestris - Sat, 2 Sep 2023 20:40 UTC

On 01/09/2023 19:42, Tim Rentsch wrote:
> In fact it must be a unique address, distinct from any other
> address that has been allocated but not yet freed.
>
> The key point is that, when the size is zero, the allocation
> routine allocates/address space/ but does not allocate/memory/.
> The address is not mapped to any memory, either physical or
> virtual.
>
> In a 64-bit address space, there are a lot more addresses than
> there are bytes of memory, even virtual bytes of memory. All but
> a very small fraction of 64-bit addresses cannot map to actual
> memory, because there is no room on the address bus (figuratively
> speaking).
>
> For example, suppose our zero byte block reside at addresses
> in the range
>
> 0x800000000000 to
> 0x802000000000
>
> and "objects" are aligned to 32-byte boundaries. That means
> there are 4294967296 zero-byte "objects" available. For each of
> those objects, we need to know if it has been allocated by not
> yet freed, that is, one bit per object. Add in a few extra
> pointers and counts to manage the space to hold the bitmap
> (so we don't have to use much more space for the bitmap than
> is necessary), and we get a little over one bit /of actual
> memory/ that is used to manage the zero-byte objects, which
> take no actual memory.
>
> Does that make more sense now?

Yes, it does. Thank you (and Kaz).

Andy

Re: The return value of realloc(p,0)

<86zg23r6fi.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Sun, 03 Sep 2023 01:45:37 -0700
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <86zg23r6fi.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me> <86sf7xspk3.fsf@linuxsc.com> <JCqIM.79177$m8Ke.22576@fx08.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="6ef63087f855104b64afdd13e03f4862";
logging-data="896175"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/SfRkZuoqIr9Em1UU9YMb/t57o2x8ddQs="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:B7/rSy2v2WAM3dnLtpNAhxt8OOg=
sha1:/h23ah9qHIBgkyXFaW2Ao8kE3nE=
 by: Tim Rentsch - Sun, 3 Sep 2023 08:45 UTC

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

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Vir Campestris <vir.campestris@invalid.invalid> writes:
>>
>>
>> In a 64-bit address space, there are a lot more addresses than
>> there are bytes of memory, even virtual bytes of memory. All but
>> a very small fraction of 64-bit addresses cannot map to actual
>> memory, because there is no room on the address bus (figuratively
>> speaking).
>
> Current processors support between 44 and 52 bits of physical
> address, and for the most part, the same on the virtual address
> space as well. ARMv8 allows the VA space to be anywhere
> from 16 bits to 52 bits on a per-translation-context) basis;
> where smaller VA sizes can significantly decrease translation
> table walk cost (i.e. by skipping levels).
>
> With DDR5 and HBM we're seeing terabytes of DRAM on the bus.

Still plenty of address space left over.

> With CXL (and Intel's PUMA chip) the physical address bus
> will be fully utilized (all 64 bits) in the not so distant
> future, if perhaps sparsely by using higher order bits to
> identify nodes on the fabric.

I think the key word there is sparsely. A very small
fraction of a 64-bit address space suffices, and that
needn't be all contiguous either - it's easy to handle
a modest number of distinct areas, without too much
burden either in space or time.

Re: The return value of realloc(p,0)

<0g2JM.580001$U3w1.358152@fx09.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx09.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: The return value of realloc(p,0)
Newsgroups: comp.lang.c
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me> <86sf7xspk3.fsf@linuxsc.com> <JCqIM.79177$m8Ke.22576@fx08.iad> <86zg23r6fi.fsf@linuxsc.com>
Lines: 43
Message-ID: <0g2JM.580001$U3w1.358152@fx09.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sun, 03 Sep 2023 16:12:44 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sun, 03 Sep 2023 16:12:44 GMT
X-Received-Bytes: 2699
 by: Scott Lurndal - Sun, 3 Sep 2023 16:12 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>scott@slp53.sl.home (Scott Lurndal) writes:
>
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>
>>> Vir Campestris <vir.campestris@invalid.invalid> writes:
>>>
>>>
>>> In a 64-bit address space, there are a lot more addresses than
>>> there are bytes of memory, even virtual bytes of memory. All but
>>> a very small fraction of 64-bit addresses cannot map to actual
>>> memory, because there is no room on the address bus (figuratively
>>> speaking).
>>
>> Current processors support between 44 and 52 bits of physical
>> address, and for the most part, the same on the virtual address
>> space as well. ARMv8 allows the VA space to be anywhere
>> from 16 bits to 52 bits on a per-translation-context) basis;
>> where smaller VA sizes can significantly decrease translation
>> table walk cost (i.e. by skipping levels).
>>
>> With DDR5 and HBM we're seeing terabytes of DRAM on the bus.
>
>Still plenty of address space left over.
>
>> With CXL (and Intel's PUMA chip) the physical address bus
>> will be fully utilized (all 64 bits) in the not so distant
>> future, if perhaps sparsely by using higher order bits to
>> identify nodes on the fabric.
>
>I think the key word there is sparsely. A very small
>fraction of a 64-bit address space suffices, and that
>needn't be all contiguous either - it's easy to handle
>a modest number of distinct areas, without too much
>burden either in space or time.

Perhaps, but vis-a-vis realloc(p,0), I'd say just don't
do that rather than adding an arcane 'zero-byte object'
concept. Deprecate the feature completely.

In fact, I'd suggest using malloc(), memcpy()/and free() if
necessary to reallocate something.

Re: The return value of realloc(p,0)

<86a5u3qdwt.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Sun, 03 Sep 2023 12:01:38 -0700
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <86a5u3qdwt.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me> <86sf7xspk3.fsf@linuxsc.com> <JCqIM.79177$m8Ke.22576@fx08.iad> <86zg23r6fi.fsf@linuxsc.com> <0g2JM.580001$U3w1.358152@fx09.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="6ef63087f855104b64afdd13e03f4862";
logging-data="1104452"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hpaZWy46Kzxj2ZLfloKR7CQGVEQnM1oI="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:PtEsU9GArRgY3r1jJmkR1Y+PrQ8=
sha1:JebZ8jZX9ybyZ/yYwQcut2bwPkY=
 by: Tim Rentsch - Sun, 3 Sep 2023 19:01 UTC

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

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

[use of address space for zero-byte allocated objects]

>> I think the key word there is sparsely. A very small
>> fraction of a 64-bit address space suffices, and that
>> needn't be all contiguous either - it's easy to handle
>> a modest number of distinct areas, without too much
>> burden either in space or time.
>
> Perhaps, but vis-a-vis realloc(p,0), I'd say just don't
> do that rather than adding an arcane 'zero-byte object'
> concept. Deprecate the feature completely.

Probably a lot of other people agree with you, but the problem
is not just realloc(). A call of malloc(0) has the same problem.
And that problem is not going away - the C standard gives an
implementation-defined choice as to whether malloc(0) returns
NULL or a pointer to "a zero-sized object" (my phrasing).

> In fact, I'd suggest using malloc(), memcpy()/and free() if
> necessary to reallocate something.

I think that idea is nutty. The realloc() function serves a
useful purpose, including better utilization of memory. If
someone wants to avoid the weird possibilities of zero-sized
objects, it's easy enough to program around that without
having to completely give up on realloc().

Re: The return value of realloc(p,0)

<865y4rqdu3.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Sun, 03 Sep 2023 12:03:16 -0700
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <865y4rqdu3.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <20230901095453.20@kylheku.com> <uct8mu$3pgff$1@dont-email.me> <86bkelsou0.fsf@linuxsc.com> <20230901120930.698@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="6ef63087f855104b64afdd13e03f4862";
logging-data="1104452"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hYFdjCBkuNHNbP5kvdbDIjf0Sv+kLWX4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:7/abk/99VyplclX+zrANXsvRX1M=
sha1:lKEZSc1jJxLVdR+yhsUrUn/cETI=
 by: Tim Rentsch - Sun, 3 Sep 2023 19:03 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:

> On 2023-09-01, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
>> The idea that zero-byte objects can all be the same doesn't
>> satisfy the specifications in the C standard.
>
> yes; we all know are talking about an alternative requiremnt
> that is not one of the current two implementation-defined choices.
>
> However, anyone can implement the idea (or others) in an allocator
> wrapper.

Yes, anyone can, but no one does, because it's a lame idea.

Re: The return value of realloc(p,0)

<w37JM.963104$TPw2.634145@fx17.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx17.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: The return value of realloc(p,0)
Newsgroups: comp.lang.c
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me> <86sf7xspk3.fsf@linuxsc.com> <JCqIM.79177$m8Ke.22576@fx08.iad> <86zg23r6fi.fsf@linuxsc.com> <0g2JM.580001$U3w1.358152@fx09.iad> <86a5u3qdwt.fsf@linuxsc.com>
Lines: 42
Message-ID: <w37JM.963104$TPw2.634145@fx17.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sun, 03 Sep 2023 21:40:44 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sun, 03 Sep 2023 21:40:44 GMT
X-Received-Bytes: 2778
 by: Scott Lurndal - Sun, 3 Sep 2023 21:40 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>scott@slp53.sl.home (Scott Lurndal) writes:
>
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>[use of address space for zero-byte allocated objects]
>
>>> I think the key word there is sparsely. A very small
>>> fraction of a 64-bit address space suffices, and that
>>> needn't be all contiguous either - it's easy to handle
>>> a modest number of distinct areas, without too much
>>> burden either in space or time.
>>
>> Perhaps, but vis-a-vis realloc(p,0), I'd say just don't
>> do that rather than adding an arcane 'zero-byte object'
>> concept. Deprecate the feature completely.
>
>Probably a lot of other people agree with you, but the problem
>is not just realloc(). A call of malloc(0) has the same problem.
>And that problem is not going away - the C standard gives an
>implementation-defined choice as to whether malloc(0) returns
>NULL or a pointer to "a zero-sized object" (my phrasing).
>
>> In fact, I'd suggest using malloc(), memcpy()/and free() if
>> necessary to reallocate something.
>
>I think that idea is nutty. The realloc() function serves a
>useful purpose, including better utilization of memory. If
>someone wants to avoid the weird possibilities of zero-sized
>objects, it's easy enough to program around that without
>having to completely give up on realloc().

I've never used it (realloc) in forty-plus years of C programming.

As for better utilization of memory, there may be cases where
a growth request can be fulfilled by expanding the existing
allocation (e.g. glibc _int_realloc). I'd think that would be
less likely in modern threaded environments, although I'd like
to see some real workload data, perhaps by instrumenting
_int_realloc to keep statistics on the frequency of cases
where the memcpy is not required.

Re: The return value of realloc(p,0)

<Pe98Ajw9UVrFjjOKy@bongo-ra.co>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: spibou@gmail.com (Spiros Bousbouras)
Newsgroups: comp.lang.c
Subject: Re: The return value of realloc(p,0)
Date: Sun, 3 Sep 2023 23:08:33 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 5
Message-ID: <Pe98Ajw9UVrFjjOKy@bongo-ra.co>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net>
<86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com>
<ucsejp$3nrkk$5@dont-email.me> <86sf7xspk3.fsf@linuxsc.com> <JCqIM.79177$m8Ke.22576@fx08.iad>
<86zg23r6fi.fsf@linuxsc.com> <0g2JM.580001$U3w1.358152@fx09.iad> <86a5u3qdwt.fsf@linuxsc.com>
<w37JM.963104$TPw2.634145@fx17.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 3 Sep 2023 23:08:33 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="14c909e5e0bb391cf5e3b9e4113bca6b";
logging-data="1179422"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/sB72fmiirGFOOjShNWApD"
Cancel-Lock: sha1:U/cZiDVb8BYfThWBZyuVwXJpGc4=
In-Reply-To: <w37JM.963104$TPw2.634145@fx17.iad>
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
 by: Spiros Bousbouras - Sun, 3 Sep 2023 23:08 UTC

On Sun, 03 Sep 2023 21:40:44 GMT
scott@slp53.sl.home (Scott Lurndal) wrote:
> I've never used it (realloc) in forty-plus years of C programming.

I use it all the time.

Re: The return value of realloc(p,0)

<8b386575-5e87-4784-ab0a-a6530acbd500n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1805:b0:40f:e2a5:3100 with SMTP id t5-20020a05622a180500b0040fe2a53100mr291237qtc.6.1693783430722;
Sun, 03 Sep 2023 16:23:50 -0700 (PDT)
X-Received: by 2002:a17:903:2309:b0:1c1:fbec:bc39 with SMTP id
d9-20020a170903230900b001c1fbecbc39mr3139431plh.4.1693783430429; Sun, 03 Sep
2023 16:23:50 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 3 Sep 2023 16:23:49 -0700 (PDT)
In-Reply-To: <Pe98Ajw9UVrFjjOKy@bongo-ra.co>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:d1a7:af75:4963:9499;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:d1a7:af75:4963:9499
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad>
<kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com>
<ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com>
<ucsejp$3nrkk$5@dont-email.me> <86sf7xspk3.fsf@linuxsc.com>
<JCqIM.79177$m8Ke.22576@fx08.iad> <86zg23r6fi.fsf@linuxsc.com>
<0g2JM.580001$U3w1.358152@fx09.iad> <86a5u3qdwt.fsf@linuxsc.com>
<w37JM.963104$TPw2.634145@fx17.iad> <Pe98Ajw9UVrFjjOKy@bongo-ra.co>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <8b386575-5e87-4784-ab0a-a6530acbd500n@googlegroups.com>
Subject: Re: The return value of realloc(p,0)
From: malcolm.arthur.mclean@gmail.com (Malcolm McLean)
Injection-Date: Sun, 03 Sep 2023 23:23:50 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Malcolm McLean - Sun, 3 Sep 2023 23:23 UTC

On Monday, 4 September 2023 at 00:08:47 UTC+1, Spiros Bousbouras wrote:
> On Sun, 03 Sep 2023 21:40:44 GMT
> sc...@slp53.sl.home (Scott Lurndal) wrote:
> > I've never used it (realloc) in forty-plus years of C programming.
> I use it all the time.
>
It's used heavily for data input where the data comes in a stream with a sentinel
indicating end of data. Particularly if the stream cannot be guaranteed seekable.

It's seldom needed for core algorithms, because normally there's a fairly tight upper
bound on the memory space required.

Re: The return value of realloc(p,0)

<86sf7uq01g.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!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: The return value of realloc(p,0)
Date: Sun, 03 Sep 2023 17:01:15 -0700
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <86sf7uq01g.fsf@linuxsc.com>
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me> <86sf7xspk3.fsf@linuxsc.com> <JCqIM.79177$m8Ke.22576@fx08.iad> <86zg23r6fi.fsf@linuxsc.com> <0g2JM.580001$U3w1.358152@fx09.iad> <86a5u3qdwt.fsf@linuxsc.com> <w37JM.963104$TPw2.634145@fx17.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="e731bbb82483d3b3451e3d39a3921e01";
logging-data="1191843"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX198eH11kFjXpcKhHhmb+rWVaMZFpBPCH7o="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:vxLBsQuajYLlxfhN1xLiX3aGAJ8=
sha1:kVMvytBygtKx+rgnHGPdjQjNEyY=
 by: Tim Rentsch - Mon, 4 Sep 2023 00:01 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:
>>
>> [use of address space for zero-byte allocated objects]
>>
>>>> I think the key word there is sparsely. A very small
>>>> fraction of a 64-bit address space suffices, and that
>>>> needn't be all contiguous either - it's easy to handle
>>>> a modest number of distinct areas, without too much
>>>> burden either in space or time.
>>>
>>> Perhaps, but vis-a-vis realloc(p,0), I'd say just don't
>>> do that rather than adding an arcane 'zero-byte object'
>>> concept. Deprecate the feature completely.
>>
>> Probably a lot of other people agree with you, but the problem
>> is not just realloc(). A call of malloc(0) has the same problem.
>> And that problem is not going away - the C standard gives an
>> implementation-defined choice as to whether malloc(0) returns
>> NULL or a pointer to "a zero-sized object" (my phrasing).
>>
>>> In fact, I'd suggest using malloc(), memcpy()/and free() if
>>> necessary to reallocate something.
>>
>> I think that idea is nutty. The realloc() function serves a
>> useful purpose, including better utilization of memory. If
>> someone wants to avoid the weird possibilities of zero-sized
>> objects, it's easy enough to program around that without
>> having to completely give up on realloc().
>
> I've never used it (realloc) in forty-plus years of C programming.
>
> As for better utilization of memory, there may be cases where
> a growth request can be fulfilled by expanding the existing
> allocation (e.g. glibc _int_realloc). I'd think that would be
> less likely in modern threaded environments, although I'd like
> to see some real workload data, perhaps by instrumenting
> _int_realloc to keep statistics on the frequency of cases
> where the memcpy is not required.

Obviously it depends on how much memory is available, and how
close to the memory limits the program is running. If we have an
environment with only, say, 1.5 G available, and we want to grow
a 0.8 G block to 1 G, the malloc/copy/free approach will fail,
but using realloc may very well succeed. Even shrinking a memory
block can fail under the malloc/copy/free approach, but will
almost certainly succeed when using realloc. Clearly there are
some potential upsides to using realloc, and I don't see any
downsides. So I still don't understand your position, unless
you're saying this is what you've always done and you don't see
any reason to change it, even if it might be suboptimal.

Re: The return value of realloc(p,0)

<RKlJM.780912$mPI2.44757@fx15.iad>

  copy mid

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

  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!fx15.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: The return value of realloc(p,0)
Newsgroups: comp.lang.c
References: <ua63if$2u50c$2@news.xmission.com> <DeyxM.134678$ens9.28488@fx45.iad> <kl972jF24chU3@mid.individual.net> <86edjju668.fsf@linuxsc.com> <ucqrn0$3dvle$1@dont-email.me> <86wmxas6ty.fsf@linuxsc.com> <ucsejp$3nrkk$5@dont-email.me> <86sf7xspk3.fsf@linuxsc.com> <JCqIM.79177$m8Ke.22576@fx08.iad> <86zg23r6fi.fsf@linuxsc.com> <0g2JM.580001$U3w1.358152@fx09.iad> <86a5u3qdwt.fsf@linuxsc.com> <w37JM.963104$TPw2.634145@fx17.iad> <86sf7uq01g.fsf@linuxsc.com>
Lines: 65
Message-ID: <RKlJM.780912$mPI2.44757@fx15.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 04 Sep 2023 14:22:41 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 04 Sep 2023 14:22:41 GMT
X-Received-Bytes: 4140
 by: Scott Lurndal - Mon, 4 Sep 2023 14:22 UTC

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:
>>>
>>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>
>>> [use of address space for zero-byte allocated objects]
>>>
>>>>> I think the key word there is sparsely. A very small
>>>>> fraction of a 64-bit address space suffices, and that
>>>>> needn't be all contiguous either - it's easy to handle
>>>>> a modest number of distinct areas, without too much
>>>>> burden either in space or time.
>>>>
>>>> Perhaps, but vis-a-vis realloc(p,0), I'd say just don't
>>>> do that rather than adding an arcane 'zero-byte object'
>>>> concept. Deprecate the feature completely.
>>>
>>> Probably a lot of other people agree with you, but the problem
>>> is not just realloc(). A call of malloc(0) has the same problem.
>>> And that problem is not going away - the C standard gives an
>>> implementation-defined choice as to whether malloc(0) returns
>>> NULL or a pointer to "a zero-sized object" (my phrasing).
>>>
>>>> In fact, I'd suggest using malloc(), memcpy()/and free() if
>>>> necessary to reallocate something.
>>>
>>> I think that idea is nutty. The realloc() function serves a
>>> useful purpose, including better utilization of memory. If
>>> someone wants to avoid the weird possibilities of zero-sized
>>> objects, it's easy enough to program around that without
>>> having to completely give up on realloc().
>>
>> I've never used it (realloc) in forty-plus years of C programming.
>>
>> As for better utilization of memory, there may be cases where
>> a growth request can be fulfilled by expanding the existing
>> allocation (e.g. glibc _int_realloc). I'd think that would be
>> less likely in modern threaded environments, although I'd like
>> to see some real workload data, perhaps by instrumenting
>> _int_realloc to keep statistics on the frequency of cases
>> where the memcpy is not required.
>
>Obviously it depends on how much memory is available, and how
>close to the memory limits the program is running. If we have an
>environment with only, say, 1.5 G available, and we want to grow
>a 0.8 G block to 1 G, the malloc/copy/free approach will fail,
>but using realloc may very well succeed. Even shrinking a memory
>block can fail under the malloc/copy/free approach, but will
>almost certainly succeed when using realloc. Clearly there are
>some potential upsides to using realloc, and I don't see any
>downsides. So I still don't understand your position, unless
>you're saying this is what you've always done and you don't see
>any reason to change it, even if it might be suboptimal.

Most of the code I've worked on has been bare metal (operating systems,
hypervisors, run-time firmware) where dynamic allocation is
either discouraged or managed with fixed size pool allocators or
slab allocators, et cetera. I suppose that's influenced my thinking about
the desirability of realloc() and run-time heap management.

Clearly others have found it useful.


devel / comp.lang.c / Re: The return value of realloc(p,0)

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor