Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Wait! You have not been prepared! -- Mr. Atoz, "Tomorrow is Yesterday", stardate 3113.2


devel / comp.lang.c / Re: on why declare a struct with a single array in it

SubjectAuthor
* on why declare a struct with a single array in itMeredith Montgomery
+* Re: on why declare a struct with a single array in itGuillaume
|`* Re: on why declare a struct with a single array in itBen Bacarisse
| `* Re: on why declare a struct with a single array in itDavid Brown
|  `* Re: on why declare a struct with a single array in itpozz
|   `* Re: on why declare a struct with a single array in itDavid Brown
|    `* Re: on why declare a struct with a single array in itManfred
|     `* Re: on why declare a struct with a single array in itpozz
|      +* Re: on why declare a struct with a single array in itManfred
|      |`- Re: on why declare a struct with a single array in itMeredith Montgomery
|      `* Re: on why declare a struct with a single array in itDavid Brown
|       `* Re: on why declare a struct with a single array in itGuillaume
|        `* Re: on why declare a struct with a single array in itKeith Thompson
|         `* Re: on why declare a struct with a single array in itScott Lurndal
|          +* Re: on why declare a struct with a single array in itKeith Thompson
|          |+* Re: on why declare a struct with a single array in itDavid Brown
|          ||+- Re: on why declare a struct with a single array in itBart
|          ||`* Re: on why declare a struct with a single array in itKeith Thompson
|          || +* Re: on why declare a struct with a single array in itBart
|          || |+* Re: on why declare a struct with a single array in itRichard Damon
|          || ||`* Re: on why declare a struct with a single array in itDavid Brown
|          || || +* Re: on why declare a struct with a single array in itManfred
|          || || |`* Re: on why declare a struct with a single array in itDavid Brown
|          || || | +* Re: on why declare a struct with a single array in itManfred
|          || || | |+* Re: on why declare a struct with a single array in itDavid Brown
|          || || | ||`* Re: on why declare a struct with a single array in itMalcolm McLean
|          || || | || `- Re: on why declare a struct with a single array in itDavid Brown
|          || || | |+- Re: on why declare a struct with a single array in itBart
|          || || | |`* Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | +* Re: on why declare a struct with a single array in itBart
|          || || | | |+- Re: on why declare a struct with a single array in itDavid Brown
|          || || | | |`* Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | | +* Re: on why declare a struct with a single array in itBart
|          || || | | | |+- Re: on why declare a struct with a single array in itBart
|          || || | | | |`* Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | | | `- Re: on why declare a struct with a single array in itBart
|          || || | | | +- Re: on why declare a struct with a single array in itChris M. Thomasson
|          || || | | | `* Re: on why declare a struct with a single array in itKenny McCormack
|          || || | | |  `* Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | |   `* Re: on why declare a struct with a single array in itKenny McCormack
|          || || | | |    +* Re: on why declare a struct with a single array in itDick
|          || || | | |    |+* Re: on why declare a struct with a single array in itKenny McCormack
|          || || | | |    ||`- Re: on why declare a struct with a single array in itMalcolm McLean
|          || || | | |    |+- Re: on why declare a struct with a single array in itDavid Brown
|          || || | | |    |`* Re: on why declare a struct with a single array in itRobert Latest
|          || || | | |    | `- Re: on why declare a struct with a single array in itKenny McCormack
|          || || | | |    `* Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | |     +* Re: on why declare a struct with a single array in itKenny McCormack
|          || || | | |     |`- Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | |     `- Re: on why declare a struct with a single array in itKeith Thompson
|          || || | | `- Re: on why declare a struct with a single array in itKeith Thompson
|          || || | `* Re: on why declare a struct with a single array in itGuillaume
|          || || |  `* Re: on why declare a struct with a single array in itBart
|          || || |   `- Re: on why declare a struct with a single array in itKeith Thompson
|          || || `- Re: on why declare a struct with a single array in itChris M. Thomasson
|          || |+* Re: on why declare a struct with a single array in itRichard Damon
|          || ||`- Re: on why declare a struct with a single array in itBart
|          || |+* Re: on why declare a struct with a single array in itJoe Pfeiffer
|          || ||`* Re: on why declare a struct with a single array in itBart
|          || || +* Re: on why declare a struct with a single array in itJoe Pfeiffer
|          || || |`- Re: on why declare a struct with a single array in itBart
|          || || `- Re: on why declare a struct with a single array in itChris M. Thomasson
|          || |`- Re: on why declare a struct with a single array in itBart
|          || +* Re: on why declare a struct with a single array in itGuillaume
|          || |+- Re: on why declare a struct with a single array in itBart
|          || |`- Re: on why declare a struct with a single array in itChris M. Thomasson
|          || `* Re: on why declare a struct with a single array in itDavid Brown
|          ||  `- Re: on why declare a struct with a single array in itChris M. Thomasson
|          |`- Re: on why declare a struct with a single array in itScott Lurndal
|          `- Re: on why declare a struct with a single array in itKenny McCormack
+* Re: on why declare a struct with a single array in itJorgen Grahn
|`- Re: on why declare a struct with a single array in itGuillaume
+- Re: on why declare a struct with a single array in itKaz Kylheku
`* Re: on why declare a struct with a single array in itAnton Shepelev
 `- Re: on why declare a struct with a single array in itMeredith Montgomery

Pages:123
Re: on why declare a struct with a single array in it

<snc47j$12d0$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19104&group=comp.lang.c#19104

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!5Eb5Kjev2xuaeX7DMmY97A.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 01:34:52 +0100
Organization: Aioe.org NNTP Server
Message-ID: <snc47j$12d0$1@gioia.aioe.org>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="35232"; posting-host="5Eb5Kjev2xuaeX7DMmY97A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
Content-Language: fr
X-Notice: Filtered by postfilter v. 0.9.2
 by: Guillaume - Sun, 21 Nov 2021 00:34 UTC

Le 21/11/2021 à 00:52, Keith Thompson a écrit :
> David Brown <david.brown@hesbynett.no> writes:
>> On 19/11/2021 21:52, Keith Thompson wrote:
> [...]
>>> Perhaps -- but typically that information is not stored in the pointer
>>> itself. If it were, all void* values would have to store that extra
>>> information.
>>>
>>> (I think it's easier to store bookkeeping data before the allocated
>>> space. If you store it after the space, it's hard to know where it is.)
>>
>> That's the most common way for a simple malloc/free system.
>>
>> It has always struck me as a mistake that C did things that way - in my
>> opinion, "free" should have taken an additional parameter of the size
>> for the deallocation.
>
> Meh. That would have introduced another rich source of programming
> errors (passing the wrong size).

Yes absolutely. Besides, the extra data allocated for each malloc() is a
bunch of stuff to identify the chain of allocated blocks, so this extra
data is needed anyway.

Re: on why declare a struct with a single array in it

<snc51m$epu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19105&group=comp.lang.c#19105

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 00:48:56 +0000
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <snc51m$epu$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc47j$12d0$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Nov 2021 00:48:54 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2dd6f070aa435e5fade94a2f2235e0fb";
logging-data="15166"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+44SKq1BTMjEFD8iYqdr4W2elOEDHtaGs="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:AkJ+kfl3ZUQMsoYdfs5TkcYDtTs=
In-Reply-To: <snc47j$12d0$1@gioia.aioe.org>
 by: Bart - Sun, 21 Nov 2021 00:48 UTC

On 21/11/2021 00:34, Guillaume wrote:
> Le 21/11/2021 à 00:52, Keith Thompson a écrit :
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 19/11/2021 21:52, Keith Thompson wrote:
>> [...]
>>>> Perhaps -- but typically that information is not stored in the pointer
>>>> itself.  If it were, all void* values would have to store that extra
>>>> information.
>>>>
>>>> (I think it's easier to store bookkeeping data before the allocated
>>>> space.  If you store it after the space, it's hard to know where it
>>>> is.)
>>>
>>> That's the most common way for a simple malloc/free system.
>>>
>>> It has always struck me as a mistake that C did things that way - in my
>>> opinion, "free" should have taken an additional parameter of the size
>>> for the deallocation.
>>
>> Meh.  That would have introduced another rich source of programming
>> errors (passing the wrong size).
>
> Yes absolutely. Besides, the extra data allocated for each malloc() is a
> bunch of stuff to identify the chain of allocated blocks, so this extra
> data is needed anyway.
>

Why for allocated blocks? The chain of freed blocks would be more
relevant, and there you can just use the freed data space.

Re: on why declare a struct with a single array in it

<snc5jo$fq4$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19106&group=comp.lang.c#19106

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sat, 20 Nov 2021 16:58:31 -0800
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <snc5jo$fq4$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc47j$12d0$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Nov 2021 00:58:32 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="7373a2dcc5666e75d4ac6c6cf178f906";
logging-data="16196"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/yVF2c0Gh67KqlzVLH1NvNV69aMrEibu8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
Cancel-Lock: sha1:j1pnrn1bP0VphBrYeBBKOqvb2Io=
In-Reply-To: <snc47j$12d0$1@gioia.aioe.org>
Content-Language: en-US
 by: Chris M. Thomasson - Sun, 21 Nov 2021 00:58 UTC

On 11/20/2021 4:34 PM, Guillaume wrote:
> Le 21/11/2021 à 00:52, Keith Thompson a écrit :
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 19/11/2021 21:52, Keith Thompson wrote:
>> [...]
>>>> Perhaps -- but typically that information is not stored in the pointer
>>>> itself.  If it were, all void* values would have to store that extra
>>>> information.
>>>>
>>>> (I think it's easier to store bookkeeping data before the allocated
>>>> space.  If you store it after the space, it's hard to know where it
>>>> is.)
>>>
>>> That's the most common way for a simple malloc/free system.
>>>
>>> It has always struck me as a mistake that C did things that way - in my
>>> opinion, "free" should have taken an additional parameter of the size
>>> for the deallocation.
>>
>> Meh.  That would have introduced another rich source of programming
>> errors (passing the wrong size).
>
> Yes absolutely. Besides, the extra data allocated for each malloc() is a
> bunch of stuff to identify the chain of allocated blocks, so this extra
> data is needed anyway.
>

It is not needed in some allocators. I have coded them with basically
zero overhead. Actually, only allocations less than sizeof(void*) are
padded up to the size of a pointer...

Re: on why declare a struct with a single array in it

<KvhmJ.55056$np6.2360@fx46.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19107&group=comp.lang.c#19107

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx46.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0)
Gecko/20100101 Thunderbird/91.3.2
Subject: Re: on why declare a struct with a single array in it
Content-Language: en-US
Newsgroups: comp.lang.c
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <snc3a0$6gs$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 54
Message-ID: <KvhmJ.55056$np6.2360@fx46.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Sat, 20 Nov 2021 20:44:10 -0500
X-Received-Bytes: 3340
 by: Richard Damon - Sun, 21 Nov 2021 01:44 UTC

On 11/20/21 7:19 PM, Bart wrote:
> On 20/11/2021 23:52, Keith Thompson wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 19/11/2021 21:52, Keith Thompson wrote:
>> [...]
>>>> Perhaps -- but typically that information is not stored in the pointer
>>>> itself.  If it were, all void* values would have to store that extra
>>>> information.
>>>>
>>>> (I think it's easier to store bookkeeping data before the allocated
>>>> space.  If you store it after the space, it's hard to know where it
>>>> is.)
>>>
>>> That's the most common way for a simple malloc/free system.
>>>
>>> It has always struck me as a mistake that C did things that way - in my
>>> opinion, "free" should have taken an additional parameter of the size
>>> for the deallocation.
>>
>> Meh.  That would have introduced another rich source of programming
>> errors (passing the wrong size).
>
> You could also pass the wrong size to malloc, or the wrong pointer to free.
>
>> Probably most implementations would
>> have kept track of the size anyway, so they could detect errors.
>
> I doubt that, unless it's for debugging mode.
>
> If an application needs to allocate 100 million 32-byte nodes, which
> would occupy 3.2GB by themselves, how much extra would be needed for the
> size, 'just for checking'?
>
> On my machine that seems to be 16 bytes per node, so 50% more. 1.6GB
> extra just so someone can write:
>
>    free(p);
>
> instead of:
>
>    free(p, sizeof(treenode));
>
> possibly in just one place in the program.
>
> It doesn't make sense.
>

Maybe not to you, but I would sure hate to have to keep track of all
that data, and would likely just wrap this dumb malloc with something
smarter.

If you need a lot of a fixed sized blocks, there are lots of algorithms
that work well for that with much less or zero overhead (if the blocks
are big enough for a pointer)

Re: on why declare a struct with a single array in it

<LvhmJ.55057$np6.17637@fx46.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19108&group=comp.lang.c#19108

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx46.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0)
Gecko/20100101 Thunderbird/91.3.2
Subject: Re: on why declare a struct with a single array in it
Content-Language: en-US
Newsgroups: comp.lang.c
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <snc3a0$6gs$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 54
Message-ID: <LvhmJ.55057$np6.17637@fx46.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Sat, 20 Nov 2021 20:44:10 -0500
X-Received-Bytes: 3341
 by: Richard Damon - Sun, 21 Nov 2021 01:44 UTC

On 11/20/21 7:19 PM, Bart wrote:
> On 20/11/2021 23:52, Keith Thompson wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 19/11/2021 21:52, Keith Thompson wrote:
>> [...]
>>>> Perhaps -- but typically that information is not stored in the pointer
>>>> itself.  If it were, all void* values would have to store that extra
>>>> information.
>>>>
>>>> (I think it's easier to store bookkeeping data before the allocated
>>>> space.  If you store it after the space, it's hard to know where it
>>>> is.)
>>>
>>> That's the most common way for a simple malloc/free system.
>>>
>>> It has always struck me as a mistake that C did things that way - in my
>>> opinion, "free" should have taken an additional parameter of the size
>>> for the deallocation.
>>
>> Meh.  That would have introduced another rich source of programming
>> errors (passing the wrong size).
>
> You could also pass the wrong size to malloc, or the wrong pointer to free.
>
>> Probably most implementations would
>> have kept track of the size anyway, so they could detect errors.
>
> I doubt that, unless it's for debugging mode.
>
> If an application needs to allocate 100 million 32-byte nodes, which
> would occupy 3.2GB by themselves, how much extra would be needed for the
> size, 'just for checking'?
>
> On my machine that seems to be 16 bytes per node, so 50% more. 1.6GB
> extra just so someone can write:
>
>    free(p);
>
> instead of:
>
>    free(p, sizeof(treenode));
>
> possibly in just one place in the program.
>
> It doesn't make sense.
>

Maybe not to you, but I would sure hate to have to keep track of all
that data, and would likely just wrap this dumb malloc with something
smarter.

If you need a lot of a fixed sized blocks, there are lots of algorithms
that work well for that with much less or zero overhead (if the blocks
are big enough for a pointer)

Re: on why declare a struct with a single array in it

<1bfsrqkxoz.fsf@pfeifferfamily.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19110&group=comp.lang.c#19110

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pfeif...@cs.nmsu.edu (Joe Pfeiffer)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sat, 20 Nov 2021 20:13:48 -0700
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <1bfsrqkxoz.fsf@pfeifferfamily.net>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com>
<Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="f2836aedbbc7c655a9ae360e6c5952dc";
logging-data="26235"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1963p3Jl7BfELXZya4Opcq0poLjAff2sk8="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:QUkpSepeYAmjxPqB0BqABQ+FD10=
sha1:q6jZUk1yEPtWgh6+hw3ILpRdhSU=
 by: Joe Pfeiffer - Sun, 21 Nov 2021 03:13 UTC

Bart <bc@freeuk.com> writes:

> On 20/11/2021 23:52, Keith Thompson wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 19/11/2021 21:52, Keith Thompson wrote:
>> [...]
>>>> Perhaps -- but typically that information is not stored in the pointer
>>>> itself. If it were, all void* values would have to store that extra
>>>> information.
>>>>
>>>> (I think it's easier to store bookkeeping data before the allocated
>>>> space. If you store it after the space, it's hard to know where it is.)
>>>
>>> That's the most common way for a simple malloc/free system.
>>>
>>> It has always struck me as a mistake that C did things that way - in my
>>> opinion, "free" should have taken an additional parameter of the size
>>> for the deallocation.
>> Meh. That would have introduced another rich source of programming
>> errors (passing the wrong size).
>
> You could also pass the wrong size to malloc, or the wrong pointer to free.
>
>> Probably most implementations would
>> have kept track of the size anyway, so they could detect errors.
>
> I doubt that, unless it's for debugging mode.
>
> If an application needs to allocate 100 million 32-byte nodes, which
> would occupy 3.2GB by themselves, how much extra would be needed for
> the size, 'just for checking'?

If that's what you're doing, you shouldn't be using malloc() for the
individual nodes.

> On my machine that seems to be 16 bytes per node, so 50% more. 1.6GB
> extra just so someone can write:
>
> free(p);
>
> instead of:
>
> free(p, sizeof(treenode));
>
> possibly in just one place in the program.
>
> It doesn't make sense.

Remember, you have to have all the data structures to keep track of the
allocated space so malloc() will work. I expect that requirement will
eat up close to that much space anyway.

Re: on why declare a struct with a single array in it

<snddlt$684$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19113&group=comp.lang.c#19113

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 12:22:21 +0000
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <snddlt$684$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<LvhmJ.55057$np6.17637@fx46.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Nov 2021 12:22:21 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2dd6f070aa435e5fade94a2f2235e0fb";
logging-data="6404"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ldLFH30J6KN1fFGxDRfGLtXDuhjTdOqQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:F5uI63ewTRdluJ+CVmWnJuuWHxk=
In-Reply-To: <LvhmJ.55057$np6.17637@fx46.iad>
 by: Bart - Sun, 21 Nov 2021 12:22 UTC

On 21/11/2021 01:44, Richard Damon wrote:
> On 11/20/21 7:19 PM, Bart wrote:
>> On 20/11/2021 23:52, Keith Thompson wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>> On 19/11/2021 21:52, Keith Thompson wrote:
>>> [...]
>>>>> Perhaps -- but typically that information is not stored in the pointer
>>>>> itself.  If it were, all void* values would have to store that extra
>>>>> information.
>>>>>
>>>>> (I think it's easier to store bookkeeping data before the allocated
>>>>> space.  If you store it after the space, it's hard to know where it
>>>>> is.)
>>>>
>>>> That's the most common way for a simple malloc/free system.
>>>>
>>>> It has always struck me as a mistake that C did things that way - in my
>>>> opinion, "free" should have taken an additional parameter of the size
>>>> for the deallocation.
>>>
>>> Meh.  That would have introduced another rich source of programming
>>> errors (passing the wrong size).
>>
>> You could also pass the wrong size to malloc, or the wrong pointer to
>> free.
>>
>>> Probably most implementations would
>>> have kept track of the size anyway, so they could detect errors.
>>
>> I doubt that, unless it's for debugging mode.
>>
>> If an application needs to allocate 100 million 32-byte nodes, which
>> would occupy 3.2GB by themselves, how much extra would be needed for
>> the size, 'just for checking'?
>>
>> On my machine that seems to be 16 bytes per node, so 50% more. 1.6GB
>> extra just so someone can write:
>>
>>     free(p);
>>
>> instead of:
>>
>>     free(p, sizeof(treenode));
>>
>> possibly in just one place in the program.
>>
>> It doesn't make sense.
>>
>
> Maybe not to you, but I would sure hate to have to keep track of all
> that data, and would likely just wrap this dumb malloc with something
> smarter.

I still don't understand why you have to keep track of /allocated/
blocks. Once allocated, they belong to the application; why does the
allocator need to care about them?

Once deallocated, or before they are allocated, then the allocator can
do what it likes with them. But it can use data within the blocks: it
doesn't need any extra space. (Unless the block is tiny, like one byte,
then it will have to impose a minimum size.)

> If you need a lot of a fixed sized blocks, there are lots of algorithms
> that work well for that with much less or zero overhead (if the blocks
> are big enough for a pointer)

Lots of same-sized blocks you mean? Then sure, there are all sorts of
custom allocators that could be used. Including the case where you know
that you will never free memory so there is no point wasting time and
space with housekeeping information.

But this is about the general purpose allocator provided by with malloc
and free. You can always add a 'better' allocator on top that uses extra
overheads; it's harder the other way around.

Re: on why declare a struct with a single array in it

<sndeel$atj$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19114&group=comp.lang.c#19114

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 12:35:34 +0000
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <sndeel$atj$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<1bfsrqkxoz.fsf@pfeifferfamily.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Nov 2021 12:35:33 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2dd6f070aa435e5fade94a2f2235e0fb";
logging-data="11187"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+J3FCZiTI/H+Z+FqNyvZYREQhWtucOT4k="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:MdxfHWjIIiQD/ouMxz/lM3sprhg=
In-Reply-To: <1bfsrqkxoz.fsf@pfeifferfamily.net>
 by: Bart - Sun, 21 Nov 2021 12:35 UTC

On 21/11/2021 03:13, Joe Pfeiffer wrote:
> Bart <bc@freeuk.com> writes:
>
>> On 20/11/2021 23:52, Keith Thompson wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>> On 19/11/2021 21:52, Keith Thompson wrote:
>>> [...]
>>>>> Perhaps -- but typically that information is not stored in the pointer
>>>>> itself. If it were, all void* values would have to store that extra
>>>>> information.
>>>>>
>>>>> (I think it's easier to store bookkeeping data before the allocated
>>>>> space. If you store it after the space, it's hard to know where it is.)
>>>>
>>>> That's the most common way for a simple malloc/free system.
>>>>
>>>> It has always struck me as a mistake that C did things that way - in my
>>>> opinion, "free" should have taken an additional parameter of the size
>>>> for the deallocation.
>>> Meh. That would have introduced another rich source of programming
>>> errors (passing the wrong size).
>>
>> You could also pass the wrong size to malloc, or the wrong pointer to free.
>>
>>> Probably most implementations would
>>> have kept track of the size anyway, so they could detect errors.
>>
>> I doubt that, unless it's for debugging mode.
>>
>> If an application needs to allocate 100 million 32-byte nodes, which
>> would occupy 3.2GB by themselves, how much extra would be needed for
>> the size, 'just for checking'?
>
> If that's what you're doing, you shouldn't be using malloc() for the
> individual nodes.

Why not; isn't that exactly what it's for?

And what is the alternative, to create a custom allocator?

>> On my machine that seems to be 16 bytes per node, so 50% more. 1.6GB
>> extra just so someone can write:
>>
>> free(p);
>>
>> instead of:
>>
>> free(p, sizeof(treenode));
>>
>> possibly in just one place in the program.
>>
>> It doesn't make sense.
>
> Remember, you have to have all the data structures to keep track of the
> allocated space so malloc() will work.

Everyone is saying this; no one is explaining exactly why it needs to!

Are you saying that, unless an allocated block is tagged with a size,
malloc has no idea which pool memory has been allocated and which hasn't?

I've written a few allocators over the years, they have never needed to
record the size of a block.

Re: on why declare a struct with a single array in it

<sndi8b$3ht$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19115&group=comp.lang.c#19115

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 14:40:27 +0100
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <sndi8b$3ht$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Nov 2021 13:40:27 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="fd2cea7d8890ce219ea71576e2c3ed94";
logging-data="3645"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19bfesB0d4p6cl0nw09ckrZgpV1AFGru50="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:TH1sj/gtnBzPAo5D50vlAkbOmig=
In-Reply-To: <875ysmjsfz.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: David Brown - Sun, 21 Nov 2021 13:40 UTC

On 21/11/2021 00:52, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 19/11/2021 21:52, Keith Thompson wrote:
> [...]
>>> Perhaps -- but typically that information is not stored in the pointer
>>> itself. If it were, all void* values would have to store that extra
>>> information.
>>>
>>> (I think it's easier to store bookkeeping data before the allocated
>>> space. If you store it after the space, it's hard to know where it is.)
>>
>> That's the most common way for a simple malloc/free system.
>>
>> It has always struck me as a mistake that C did things that way - in my
>> opinion, "free" should have taken an additional parameter of the size
>> for the deallocation.
>
> Meh. That would have introduced another rich source of programming
> errors (passing the wrong size). Probably most implementations would
> have kept track of the size anyway, so they could detect errors. As
> long as they're keeping track, why make the user do so as well?

It would have lead to some different programming errors, perhaps, but I
don't think it would have lead to more. If you are at the point in your
code where you can correctly call "free(p);", it seems highly unlikely
that you don't also have the size of the allocation there too. (To be
clear, I expect you to have the size used when calling malloc - if the
heap implementation rounds that up to a block size in some way, then it
would do the same for sized free.)

You know the size used in your malloc() call. Either you are getting
space for a single object (such as a large struct), or an array - and
you know the size when you are using the object or array later.

There may be some occasions when a little extra effort is needed on the
part of the programmer to keep track of the size before calling free(),
but I believe that would be a minority of cases.

>
> Perhaps a lower-level allocator might be more efficient if user code is
> required to keep track, but I don't think it would be all that useful
> for most applications.
>

It can easily make a big difference when you consider caches and pages.
A cache line might be 32 bytes long, or more - either your data is
badly aligned, or that's a lot of wasted space just to hold a size that
the client code already knows. If you've got a tree structure where the
node data (including pointers to branches) all fit within a 32 byte
block, half the space in your cache is wasted holding the sizes for
"free". For bigger cache lines, either you waste even more space per
allocation or your sizes (within their 16 or 32 byte alignment block)
are mixed with the data that you actually /want/ in your cache. However
you do it, it can be a serious waste of cache space.

The alternative is to have more advanced heap management algorithms
which keep the size information independently, meaning they have to
support a mapping between allocated addresses and the size allocated.
This is complex, slow and inefficient compared to a structure where the
client code has the size - all you need then might be a plain bitmap
with a single bit per used block (with pools to deal efficiently with
different sizes of allocation).

Storing the size of an allocation as an extra item before the block was
a simple and efficient mechanism in earlier computers - but not now.
There are, of course, many ways to implement a heap with different
balances of complexity, speed, efficiency, etc. Storing the size
somewhere does not make them simpler, faster or more efficient.

C++14 introduced sized deletion so that you can make allocators that
don't need to track sizes.

Re: on why declare a struct with a single array in it

<snditr$7l1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19116&group=comp.lang.c#19116

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 14:51:55 +0100
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <snditr$7l1$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<KvhmJ.55056$np6.2360@fx46.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Nov 2021 13:51:55 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="fd2cea7d8890ce219ea71576e2c3ed94";
logging-data="7841"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+MlBwqu/o/25n/SaOi2gh+Q7rmjw0EX80="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:8A6LgND//5eqwHVcof4pjhjZOLw=
In-Reply-To: <KvhmJ.55056$np6.2360@fx46.iad>
Content-Language: en-GB
 by: David Brown - Sun, 21 Nov 2021 13:51 UTC

On 21/11/2021 02:44, Richard Damon wrote:
> On 11/20/21 7:19 PM, Bart wrote:

>> It doesn't make sense.

I don't want to make a habit of it, but I agree with Bart here :-)

>
> Maybe not to you, but I would sure hate to have to keep track of all
> that data, and would likely just wrap this dumb malloc with something
> smarter.

And that is /fine/. For those few cases where it might be inconvenient
to know the size at deallocation time, make a wrapper so that the size
is tracked. If you need that functionality, write the code and pay the
price - others that don't need the heap to record their allocation sizes
shouldn't have to pay for your needs. It is no different from malloc
and calloc - if you want the convenience of zeroing the memory, you can
use calloc, but if you don't want it you can use malloc and not pay for
the extra effort.

Of course it is too late to make such untracked-size heap functions the
standard for C - I see it as an unlucky choice made long ago. After
all, if you had "malloc_untracked" and "free_untracked" then it would be
a simple matter to implement traditional C "malloc" and "free" on top of it.

>
> If you need a lot of a fixed sized blocks, there are lots of algorithms
> that work well for that with much less or zero overhead (if the blocks
> are big enough for a pointer)

Yes, of course there are many ways of efficiently handling fixed size
blocks. But it would be more convenient if the common heap system
didn't have this particular inefficiency.

Re: on why declare a struct with a single array in it

<sndls8$r20$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19117&group=comp.lang.c#19117

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 14:42:17 +0000
Organization: A noiseless patient Spider
Lines: 76
Message-ID: <sndls8$r20$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Nov 2021 14:42:16 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2dd6f070aa435e5fade94a2f2235e0fb";
logging-data="27712"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185p0orn+csWF+EA8akm0BQe2Y2Jiy166I="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:xXDl+WTW+q1n20TgXdJk7+MmAiA=
In-Reply-To: <snc3a0$6gs$1@dont-email.me>
 by: Bart - Sun, 21 Nov 2021 14:42 UTC

On 21/11/2021 00:19, Bart wrote:
> On 20/11/2021 23:52, Keith Thompson wrote:

>> Probably most implementations would
>> have kept track of the size anyway, so they could detect errors.
>
> I doubt that, unless it's for debugging mode.
>
> If an application needs to allocate 100 million 32-byte nodes, which
> would occupy 3.2GB by themselves, how much extra would be needed for the
> size, 'just for checking'?
>
> On my machine that seems to be 16 bytes per node, so 50% more. 1.6GB
> extra just so someone can write:
>
>    free(p);
>
> instead of:
>
>    free(p, sizeof(treenode));
>
> possibly in just one place in the program.

That last example (which would have been a Binary Trees benchmark) could
also be written as:

free(p, sizeof(*p));

If p has type T* and you know the allocation is for exactly one such
object (eg. as AST node in a compiler), then you don't need the
allocator to keep track of the size.

I usually go to some trouble to craft my struct sizes to be
powers-of-to, and it would be annoying for an allocator to just add its
own garbage on top.

(Here are the two lines from Binary Trees in my language (not using C as
I don't have my pcm library there) that handle memory:

t := malloc(treenode.bytes)
free(tree)

that's when I use C routines directly. If I switch to a different
allocator that I normally use within my interpreters, it looks like this:

t := pcm_alloc(treenode.bytes)
pcm_free(tree, treenode.bytes) # or tree^.bytes

Either work. Except that malloc/free [**] take 11 seconds for one run.

My pcm_alloc/pcm_free take 3.7 seconds, nearly 3 times as fast.

Although the reason for that is probably not the extra memory, as the
node size of 24 bytes is allocated 32 bytes in both cases: with malloc
because of the size overhead; in mine because it's rounded up to a
power-of-two.

My allocator is probably just simpler...

---------------

** Using malloc/free from msvcrt.dll.

C version using malloc/free and gcc-O3 takes 10.3 seconds, likely using
same library.

C version on WSL and with gcc-O3 takes 4.8 seconds.

However, if I port my language using those pcm calls to C, then I get
timings of 2.7 seconds on Windows, and 2.1 seconds on WSL, using gcc-O3.)

)

Re: on why declare a struct with a single array in it

<W0umJ.116417$IW4.4067@fx48.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19118&group=comp.lang.c#19118

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx48.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: on why declare a struct with a single array in it
Newsgroups: comp.lang.c
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org> <87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me> <sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me> <sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me> <sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org> <87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad> <874k87q35r.fsf@nosuchdomain.example.com>
Lines: 37
Message-ID: <W0umJ.116417$IW4.4067@fx48.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sun, 21 Nov 2021 15:58:46 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sun, 21 Nov 2021 15:58:46 GMT
X-Received-Bytes: 2791
 by: Scott Lurndal - Sun, 21 Nov 2021 15:58 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>scott@slp53.sl.home (Scott Lurndal) writes:
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>>Guillaume <message@bottle.org> writes:
>>>> Le 18/11/2021 à 22:58, David Brown a écrit :
>>
>>>> Now there are kind of exceptions to that, but that are purely
>>>> "hand-implemented" and have nothing to do with the language itself:
>>>> for instance, the typical malloc() behavior. Behind the scenes,
>>>> pointers returned by malloc() are "fat pointers", and you can also
>>>> implement your own fat pointers if you so wish. But the language
>>>> doesn't care. It has no provision for that, except you can do pointer
>>>> arithmetics to your heart's content.
>>>
>>>I'm not sure what you mean about malloc() returning "fat pointers".
>>>malloc() returns a void* with no information about the size or type of
>>>what it points to. Some implementations might provide a way to query
>>>the size of the allocated space, but that's not standard.
>>
>> Many malloc implementations reserve additional space, either before
>> or after the allocated space for bookkeeping and alignment purposes.
>>
>> Perhaps that is the source of Guillaume's "fat pointer" reference.
>
>Perhaps -- but typically that information is not stored in the pointer
>itself. If it were, all void* values would have to store that extra
>information.

True. The only modern fat pointers I'm aware of (at least that I can talk
about publically) are the Burroughs B6500 family processors (capabilities)
and the CHERI project.

>
>(I think it's easier to store bookkeeping data before the allocated
>space. If you store it after the space, it's hard to know where it is.)

Point.

Re: on why declare a struct with a single array in it

<1bbl2dl7j2.fsf@pfeifferfamily.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19120&group=comp.lang.c#19120

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pfeif...@cs.nmsu.edu (Joe Pfeiffer)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 10:53:37 -0700
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <1bbl2dl7j2.fsf@pfeifferfamily.net>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com>
<Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<1bfsrqkxoz.fsf@pfeifferfamily.net> <sndeel$atj$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="f2836aedbbc7c655a9ae360e6c5952dc";
logging-data="8293"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18GO+C+xcIzCEkkP1/pjWKayGu1J+hU1g0="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:eQ5IKz5Fv2ZLyxV1c4zPZzSD+aU=
sha1:KuTpcCF0fNUVk0DY+2obwgCCh9o=
 by: Joe Pfeiffer - Sun, 21 Nov 2021 17:53 UTC

Bart <bc@freeuk.com> writes:
> On 21/11/2021 03:13, Joe Pfeiffer wrote:
>> Bart <bc@freeuk.com> writes:
>>>
>>> If an application needs to allocate 100 million 32-byte nodes, which
>>> would occupy 3.2GB by themselves, how much extra would be needed for
>>> the size, 'just for checking'?
>> If that's what you're doing, you shouldn't be using malloc() for the
>> individual nodes.
>
> Why not; isn't that exactly what it's for?

Because it isn't optimized for a use case like that. It's a general
purpose allocator, and you're using it for a situation that's 'way out
on the fringes.

> And what is the alternative, to create a custom allocator?

Yes. When I've had similar situations I've allocated large blocks with
malloc() and then allocated my little blocks from the big ones.

>>> On my machine that seems to be 16 bytes per node, so 50% more. 1.6GB
>>> extra just so someone can write:
>>>
>>> free(p);
>>>
>>> instead of:
>>>
>>> free(p, sizeof(treenode));
>>>
>>> possibly in just one place in the program.
>>>
>>> It doesn't make sense.
>> Remember, you have to have all the data structures to keep track of
>> the
>> allocated space so malloc() will work.
>
> Everyone is saying this; no one is explaining exactly why it needs to!
>
> Are you saying that, unless an allocated block is tagged with a size,
> malloc has no idea which pool memory has been allocated and which
> hasn't?
>
> I've written a few allocators over the years, they have never needed
> to record the size of a block.

Re: on why declare a struct with a single array in it

<sne3b7$1mre$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19121&group=comp.lang.c#19121

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 19:32:07 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sne3b7$1mre$1@gioia.aioe.org>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<KvhmJ.55056$np6.2360@fx46.iad> <snditr$7l1$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="56174"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Sun, 21 Nov 2021 18:32 UTC

On 11/21/2021 2:51 PM, David Brown wrote:
> On 21/11/2021 02:44, Richard Damon wrote:
>> On 11/20/21 7:19 PM, Bart wrote:
>
>>> It doesn't make sense.
>
> I don't want to make a habit of it, but I agree with Bart here :-)
>
>>
>> Maybe not to you, but I would sure hate to have to keep track of all
>> that data, and would likely just wrap this dumb malloc with something
>> smarter.
>
> And that is /fine/. For those few cases where it might be inconvenient
> to know the size at deallocation time, make a wrapper so that the size
> is tracked. If you need that functionality, write the code and pay the
> price - others that don't need the heap to record their allocation sizes
> shouldn't have to pay for your needs. It is no different from malloc
> and calloc - if you want the convenience of zeroing the memory, you can
> use calloc, but if you don't want it you can use malloc and not pay for
> the extra effort.
>
> Of course it is too late to make such untracked-size heap functions the
> standard for C - I see it as an unlucky choice made long ago. After
> all, if you had "malloc_untracked" and "free_untracked" then it would be
> a simple matter to implement traditional C "malloc" and "free" on top of it.

The problem I see with this is that it assumes one and only type of
metadata information to be associated to the pointer, specifically the
block size.
I believe there are countless implementations of malloc out there, and I
guess this might not be the case, or it may be the case today but not
tomorrow.
The point is that if you set the interface to be pointer+size you also
put constraints on the implementation, on top of the extra burden for
the programmer.

>
>>
>> If you need a lot of a fixed sized blocks, there are lots of algorithms
>> that work well for that with much less or zero overhead (if the blocks
>> are big enough for a pointer)
>
> Yes, of course there are many ways of efficiently handling fixed size
> blocks. But it would be more convenient if the common heap system
> didn't have this particular inefficiency.
>

Re: on why declare a struct with a single array in it

<sne4cd$un$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19122&group=comp.lang.c#19122

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 18:49:50 +0000
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <sne4cd$un$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<1bfsrqkxoz.fsf@pfeifferfamily.net> <sndeel$atj$1@dont-email.me>
<1bbl2dl7j2.fsf@pfeifferfamily.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Nov 2021 18:49:49 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2dd6f070aa435e5fade94a2f2235e0fb";
logging-data="983"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19tE7qlivCnuUemXvyP0M9uakBQaYdGnMM="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:fEp5WxB8hE12E7CjP/u8arnWfeY=
In-Reply-To: <1bbl2dl7j2.fsf@pfeifferfamily.net>
 by: Bart - Sun, 21 Nov 2021 18:49 UTC

On 21/11/2021 17:53, Joe Pfeiffer wrote:
> Bart <bc@freeuk.com> writes:
>> On 21/11/2021 03:13, Joe Pfeiffer wrote:
>>> Bart <bc@freeuk.com> writes:
>>>>
>>>> If an application needs to allocate 100 million 32-byte nodes, which
>>>> would occupy 3.2GB by themselves, how much extra would be needed for
>>>> the size, 'just for checking'?
>>> If that's what you're doing, you shouldn't be using malloc() for the
>>> individual nodes.
>>
>> Why not; isn't that exactly what it's for?
>
> Because it isn't optimized for a use case like that. It's a general
> purpose allocator, and you're using it for a situation that's 'way out
> on the fringes.
>
>> And what is the alternative, to create a custom allocator?
>
> Yes. When I've had similar situations I've allocated large blocks with
> malloc() and then allocated my little blocks from the big ones.

I would expect the majority of allocs in an application (not total
memory used) are going to be small:

* Numbers of instances of identical struct types, across many types

* Allocation of short strings

Yet all these common uses, where overheads are likely to more
significant, are considered to be 'on the fringes'?

With strings in particular, the allocator holds info about the string
length (or its allocated size), then would be useful to an application,
yet there is no way to make use of that information in the application.

Which leads to the situation that for a C++-style expandable string or
vector, the necessary descriptor needs to maintain its own duplicate of
the 'allocated' size.

(Actually there will be the current length, the requested capacity,
which are known, and the space actually allocated by malloc, which is
unknown; plus the metadata it needs.)

It all seems rather wasteful.

Re: on why declare a struct with a single array in it

<snebg4$jtk$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19123&group=comp.lang.c#19123

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 21:51:16 +0100
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <snebg4$jtk$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<KvhmJ.55056$np6.2360@fx46.iad> <snditr$7l1$1@dont-email.me>
<sne3b7$1mre$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Nov 2021 20:51:16 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="fd2cea7d8890ce219ea71576e2c3ed94";
logging-data="20404"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ku3w9UUiRil09sxUlJLWR56WxpLQr6jc="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:sP54FevdfuiWZb+jgprSjH6Ut4w=
In-Reply-To: <sne3b7$1mre$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Sun, 21 Nov 2021 20:51 UTC

On 21/11/2021 19:32, Manfred wrote:
> On 11/21/2021 2:51 PM, David Brown wrote:
>> On 21/11/2021 02:44, Richard Damon wrote:
>>> On 11/20/21 7:19 PM, Bart wrote:
>>
>>>> It doesn't make sense.
>>
>> I don't want to make a habit of it, but I agree with Bart here :-)
>>
>>>
>>> Maybe not to you, but I would sure hate to have to keep track of all
>>> that data, and would likely just wrap this dumb malloc with something
>>> smarter.
>>
>> And that is /fine/.  For those few cases where it might be inconvenient
>> to know the size at deallocation time, make a wrapper so that the size
>> is tracked.  If you need that functionality, write the code and pay the
>> price - others that don't need the heap to record their allocation sizes
>> shouldn't have to pay for your needs.  It is no different from malloc
>> and calloc - if you want the convenience of zeroing the memory, you can
>> use calloc, but if you don't want it you can use malloc and not pay for
>> the extra effort.
>>
>> Of course it is too late to make such untracked-size heap functions the
>> standard for C - I see it as an unlucky choice made long ago.  After
>> all, if you had "malloc_untracked" and "free_untracked" then it would be
>> a simple matter to implement traditional C "malloc" and "free" on top
>> of it.
>
> The problem I see with this is that it assumes one and only type of
> metadata information to be associated to the pointer, specifically the
> block size.

No. It merely relieves the heap implementation from having to store the
size of the allocation. Any other metadata (such as for tracking free
space lists, or handling efficient multi-threaded access) will still be
needed.

> I believe there are countless implementations of malloc out there, and I
> guess this might not be the case, or it may be the case today but not
> tomorrow.
> The point is that if you set the interface to be pointer+size you also
> put constraints on the implementation, on top of the extra burden for
> the programmer.
>
How could this suggestion impose any constraints on the implementation?
The interface for getting the memory is the same (albeit with a
different name, in order to keep compatibility with existing code), and
the interface for freeing memory gives the implementation more
information. It would be perfectly possible (though a little unhelpful)
for an implementation of the "_untracked" functions to be :

#define malloc_untracked(size) malloc(size)
#define free_untracked(ptr, size) free(ptr)

An implementation can do better, given this new information, but there
are no new limitations or restrictions.

Re: on why declare a struct with a single array in it

<snebus$mv8$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19124&group=comp.lang.c#19124

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 12:59:05 -0800
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <snebus$mv8$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <sndi8b$3ht$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Nov 2021 20:59:08 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="7373a2dcc5666e75d4ac6c6cf178f906";
logging-data="23528"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ulyW18UIj/N6c7O1nuTVLX4WQUpjhcXs="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
Cancel-Lock: sha1:7lk2G6Mn2atPX6QKKbXSpQpverU=
In-Reply-To: <sndi8b$3ht$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sun, 21 Nov 2021 20:59 UTC

On 11/21/2021 5:40 AM, David Brown wrote:
> On 21/11/2021 00:52, Keith Thompson wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 19/11/2021 21:52, Keith Thompson wrote:
>> [...]
>>>> Perhaps -- but typically that information is not stored in the pointer
>>>> itself. If it were, all void* values would have to store that extra
>>>> information.
>>>>
>>>> (I think it's easier to store bookkeeping data before the allocated
>>>> space. If you store it after the space, it's hard to know where it is.)
>>>
>>> That's the most common way for a simple malloc/free system.
>>>
>>> It has always struck me as a mistake that C did things that way - in my
>>> opinion, "free" should have taken an additional parameter of the size
>>> for the deallocation.
>>
>> Meh. That would have introduced another rich source of programming
>> errors (passing the wrong size). Probably most implementations would
>> have kept track of the size anyway, so they could detect errors. As
>> long as they're keeping track, why make the user do so as well?
>
> It would have lead to some different programming errors, perhaps, but I
> don't think it would have lead to more. If you are at the point in your
> code where you can correctly call "free(p);", it seems highly unlikely
> that you don't also have the size of the allocation there too. (To be
> clear, I expect you to have the size used when calling malloc - if the
> heap implementation rounds that up to a block size in some way, then it
> would do the same for sized free.)
>
> You know the size used in your malloc() call. Either you are getting
> space for a single object (such as a large struct), or an array - and
> you know the size when you are using the object or array later.

[...]

Fwiw, I am quite fond of creating a big chunk of memory, and aligning it
on a "large" boundary. Say 4096*4 bytes... Now, every address in the
memory can be rounded down to get at the boundary. There is a structure
sitting right before this boundary, a header:

[header][aligned_memory]

Every memory allocation less than the size of a pointer, is rounded up
to the size of a pointer. So, on a 32-bit system, 0...3 are rounded up
to 4. That's it. Now, wrt freeing something, we take the address, round
down to boundary, subtract the header, then we are at the header that
can contain a free list to link the pointer in. It's quite nice, and can
be on a per-thread basis. headers per-thread...

;^)

Re: on why declare a struct with a single array in it

<snec8p$og6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19125&group=comp.lang.c#19125

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 13:04:25 -0800
Organization: A noiseless patient Spider
Lines: 75
Message-ID: <snec8p$og6$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<1bfsrqkxoz.fsf@pfeifferfamily.net> <sndeel$atj$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Nov 2021 21:04:25 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="7373a2dcc5666e75d4ac6c6cf178f906";
logging-data="25094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19WLRsxCvuVKAn5ERTukI7HewCBla2/GjY="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
Cancel-Lock: sha1:zUr4yKnWe4kCW41U9QN/LfM3fPc=
In-Reply-To: <sndeel$atj$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sun, 21 Nov 2021 21:04 UTC

On 11/21/2021 4:35 AM, Bart wrote:
> On 21/11/2021 03:13, Joe Pfeiffer wrote:
>> Bart <bc@freeuk.com> writes:
>>
>>> On 20/11/2021 23:52, Keith Thompson wrote:
>>>> David Brown <david.brown@hesbynett.no> writes:
>>>>> On 19/11/2021 21:52, Keith Thompson wrote:
>>>> [...]
>>>>>> Perhaps -- but typically that information is not stored in the
>>>>>> pointer
>>>>>> itself.  If it were, all void* values would have to store that extra
>>>>>> information.
>>>>>>
>>>>>> (I think it's easier to store bookkeeping data before the allocated
>>>>>> space.  If you store it after the space, it's hard to know where
>>>>>> it is.)
>>>>>
>>>>> That's the most common way for a simple malloc/free system.
>>>>>
>>>>> It has always struck me as a mistake that C did things that way -
>>>>> in my
>>>>> opinion, "free" should have taken an additional parameter of the size
>>>>> for the deallocation.
>>>> Meh.  That would have introduced another rich source of programming
>>>> errors (passing the wrong size).
>>>
>>> You could also pass the wrong size to malloc, or the wrong pointer to
>>> free.
>>>
>>>> Probably most implementations would
>>>> have kept track of the size anyway, so they could detect errors.
>>>
>>> I doubt that, unless it's for debugging mode.
>>>
>>> If an application needs to allocate 100 million 32-byte nodes, which
>>> would occupy 3.2GB by themselves, how much extra would be needed for
>>> the size, 'just for checking'?
>>
>> If that's what you're doing, you shouldn't be using malloc() for the
>> individual nodes.
>
> Why not; isn't that exactly what it's for?
>
> And what is the alternative, to create a custom allocator?
>
>>> On my machine that seems to be 16 bytes per node, so 50% more. 1.6GB
>>> extra just so someone can write:
>>>
>>>     free(p);
>>>
>>> instead of:
>>>
>>>     free(p, sizeof(treenode));
>>>
>>> possibly in just one place in the program.
>>>
>>> It doesn't make sense.
>>
>> Remember, you have to have all the data structures to keep track of the
>> allocated space so malloc() will work.
>
> Everyone is saying this; no one is explaining exactly why it needs to!
>
> Are you saying that, unless an allocated block is tagged with a size,
> malloc has no idea which pool memory has been allocated and which hasn't?

free can round its address down to a boundary, and get at a header from
there. Therefore it can find its freelist, or pool, or whatever...

>
> I've written a few allocators over the years, they have never needed to
> record the size of a block.
>

Re: on why declare a struct with a single array in it

<snecgg$og6$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19126&group=comp.lang.c#19126

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 13:08:32 -0800
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <snecgg$og6$2@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<KvhmJ.55056$np6.2360@fx46.iad> <snditr$7l1$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Nov 2021 21:08:32 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="7373a2dcc5666e75d4ac6c6cf178f906";
logging-data="25094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hV8ZwS/F80aAorgqD+0doJ9xsnoj12Mw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
Cancel-Lock: sha1:kFfqsSj7nBnDEjZmQuo5bGqNsEM=
In-Reply-To: <snditr$7l1$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sun, 21 Nov 2021 21:08 UTC

On 11/21/2021 5:51 AM, David Brown wrote:
> On 21/11/2021 02:44, Richard Damon wrote:
>> On 11/20/21 7:19 PM, Bart wrote:
>
>>> It doesn't make sense.
>
> I don't want to make a habit of it, but I agree with Bart here :-)
>
>>
>> Maybe not to you, but I would sure hate to have to keep track of all
>> that data, and would likely just wrap this dumb malloc with something
>> smarter.
>
> And that is /fine/. For those few cases where it might be inconvenient
> to know the size at deallocation time, make a wrapper so that the size
> is tracked. If you need that functionality, write the code and pay the
> price - others that don't need the heap to record their allocation sizes
> shouldn't have to pay for your needs. It is no different from malloc
> and calloc - if you want the convenience of zeroing the memory, you can
> use calloc, but if you don't want it you can use malloc and not pay for
> the extra effort.

For some reason this is making me think of a bug I found in MSVC:

https://groups.google.com/g/comp.lang.c++/c/48Tm8j8ag-0/m/VYI8rvB9vIwJ

>
> Of course it is too late to make such untracked-size heap functions the
> standard for C - I see it as an unlucky choice made long ago. After
> all, if you had "malloc_untracked" and "free_untracked" then it would be
> a simple matter to implement traditional C "malloc" and "free" on top of it.
>
>>
>> If you need a lot of a fixed sized blocks, there are lots of algorithms
>> that work well for that with much less or zero overhead (if the blocks
>> are big enough for a pointer)
>
> Yes, of course there are many ways of efficiently handling fixed size
> blocks. But it would be more convenient if the common heap system
> didn't have this particular inefficiency.
>

Re: on why declare a struct with a single array in it

<sneuje$1qm$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19127&group=comp.lang.c#19127

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Mon, 22 Nov 2021 03:17:17 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sneuje$1qm$1@gioia.aioe.org>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<KvhmJ.55056$np6.2360@fx46.iad> <snditr$7l1$1@dont-email.me>
<sne3b7$1mre$1@gioia.aioe.org> <snebg4$jtk$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="1878"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Manfred - Mon, 22 Nov 2021 02:17 UTC

On 11/21/2021 9:51 PM, David Brown wrote:
> On 21/11/2021 19:32, Manfred wrote:
>> On 11/21/2021 2:51 PM, David Brown wrote:
>>> On 21/11/2021 02:44, Richard Damon wrote:
>>>> On 11/20/21 7:19 PM, Bart wrote:
>>>
>>>>> It doesn't make sense.
>>>
>>> I don't want to make a habit of it, but I agree with Bart here :-)
>>>
>>>>
>>>> Maybe not to you, but I would sure hate to have to keep track of all
>>>> that data, and would likely just wrap this dumb malloc with something
>>>> smarter.
>>>
>>> And that is /fine/.  For those few cases where it might be inconvenient
>>> to know the size at deallocation time, make a wrapper so that the size
>>> is tracked.  If you need that functionality, write the code and pay the
>>> price - others that don't need the heap to record their allocation sizes
>>> shouldn't have to pay for your needs.  It is no different from malloc
>>> and calloc - if you want the convenience of zeroing the memory, you can
>>> use calloc, but if you don't want it you can use malloc and not pay for
>>> the extra effort.
>>>
>>> Of course it is too late to make such untracked-size heap functions the
>>> standard for C - I see it as an unlucky choice made long ago.  After
>>> all, if you had "malloc_untracked" and "free_untracked" then it would be
>>> a simple matter to implement traditional C "malloc" and "free" on top
>>> of it.
>>
>> The problem I see with this is that it assumes one and only type of
>> metadata information to be associated to the pointer, specifically the
>> block size.
>
> No. It merely relieves the heap implementation from having to store the
> size of the allocation. Any other metadata (such as for tracking free
> space lists, or handling efficient multi-threaded access) will still be
> needed.

But it means that the implementation has to manage memory blocks based
on size instead of something else - if it had to store anything else
with the pointer, delegating the size to the programmer would be pointless
One alternative that pops to mind is a pointer to the next block,
however this might work, or nothing at all and a lookup table, or
anything else I couldn't think of right now, since I am not designing
the allocator, which the committee does not design either.
I think it makes sense that the language defines the interface strictly
to fulfill the requirements, and keep as much as possible of the design
internal to the implementation.

Moreover, the requested size (used by the user) may be different from
the actual size (used by the allocator), which poses some consistency
issues - but that's a side note.

>
>> I believe there are countless implementations of malloc out there, and I
>> guess this might not be the case, or it may be the case today but not
>> tomorrow.
>> The point is that if you set the interface to be pointer+size you also
>> put constraints on the implementation, on top of the extra burden for
>> the programmer.
>>
> How could this suggestion impose any constraints on the implementation?
> The interface for getting the memory is the same (albeit with a
> different name, in order to keep compatibility with existing code), and
> the interface for freeing memory gives the implementation more
> information. It would be perfectly possible (though a little unhelpful)
> for an implementation of the "_untracked" functions to be :
>
> #define malloc_untracked(size) malloc(size)
> #define free_untracked(ptr, size) free(ptr)
>
> An implementation can do better, given this new information, but there
> are no new limitations or restrictions.
>

Well, requiring the programmer to keep track of the block size is a
significant programming overhead - if it is unused, it's a significant
waste I'd rather not pay for.

Re: on why declare a struct with a single array in it

<snf1gn$r3q$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19128&group=comp.lang.c#19128

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!5Eb5Kjev2xuaeX7DMmY97A.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Mon, 22 Nov 2021 04:06:55 +0100
Organization: Aioe.org NNTP Server
Message-ID: <snf1gn$r3q$1@gioia.aioe.org>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<KvhmJ.55056$np6.2360@fx46.iad> <snditr$7l1$1@dont-email.me>
<sne3b7$1mre$1@gioia.aioe.org> <snebg4$jtk$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="27770"; posting-host="5Eb5Kjev2xuaeX7DMmY97A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Guillaume - Mon, 22 Nov 2021 03:06 UTC

Le 21/11/2021 à 21:51, David Brown a écrit :
> No. It merely relieves the heap implementation from having to store the
> size of the allocation. Any other metadata (such as for tracking free
> space lists, or handling efficient multi-threaded access) will still be
> needed.
>
>> I believe there are countless implementations of malloc out there, and I
>> guess this might not be the case, or it may be the case today but not
>> tomorrow.

Of all the typical implementations of malloc() and similar
general-purpose allocators, I've always seen the same approach of
storing metadata either right before (with then the appropriate padding
if required), or right after the actual usable block of memory. In a
number of cases, the metadata is even duplicated both before and after
the block - for data locality reasons. This can be more efficient
cache-wise.

Re: on why declare a struct with a single array in it

<snfrib$uje$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19133&group=comp.lang.c#19133

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Mon, 22 Nov 2021 11:31:39 +0100
Organization: A noiseless patient Spider
Lines: 97
Message-ID: <snfrib$uje$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<KvhmJ.55056$np6.2360@fx46.iad> <snditr$7l1$1@dont-email.me>
<sne3b7$1mre$1@gioia.aioe.org> <snebg4$jtk$1@dont-email.me>
<sneuje$1qm$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Nov 2021 10:31:39 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="0c2ae86aa6ab7364331cb2da1f2a7b93";
logging-data="31342"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sdRTM2YSoHlysvqqlehZ25UNrWIa0Zdg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:Cs5U5vota/l58JmUyHvX6TmNGZY=
In-Reply-To: <sneuje$1qm$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Mon, 22 Nov 2021 10:31 UTC

On 22/11/2021 03:17, Manfred wrote:
> On 11/21/2021 9:51 PM, David Brown wrote:
>> On 21/11/2021 19:32, Manfred wrote:
>>> On 11/21/2021 2:51 PM, David Brown wrote:
>>>> On 21/11/2021 02:44, Richard Damon wrote:
>>>>> On 11/20/21 7:19 PM, Bart wrote:
>>>>
>>>>>> It doesn't make sense.
>>>>
>>>> I don't want to make a habit of it, but I agree with Bart here :-)
>>>>
>>>>>
>>>>> Maybe not to you, but I would sure hate to have to keep track of all
>>>>> that data, and would likely just wrap this dumb malloc with something
>>>>> smarter.
>>>>
>>>> And that is /fine/.  For those few cases where it might be inconvenient
>>>> to know the size at deallocation time, make a wrapper so that the size
>>>> is tracked.  If you need that functionality, write the code and pay the
>>>> price - others that don't need the heap to record their allocation
>>>> sizes
>>>> shouldn't have to pay for your needs.  It is no different from malloc
>>>> and calloc - if you want the convenience of zeroing the memory, you can
>>>> use calloc, but if you don't want it you can use malloc and not pay for
>>>> the extra effort.
>>>>
>>>> Of course it is too late to make such untracked-size heap functions the
>>>> standard for C - I see it as an unlucky choice made long ago.  After
>>>> all, if you had "malloc_untracked" and "free_untracked" then it
>>>> would be
>>>> a simple matter to implement traditional C "malloc" and "free" on top
>>>> of it.
>>>
>>> The problem I see with this is that it assumes one and only type of
>>> metadata information to be associated to the pointer, specifically the
>>> block size.
>>
>> No.  It merely relieves the heap implementation from having to store the
>> size of the allocation.  Any other metadata (such as for tracking free
>> space lists, or handling efficient multi-threaded access) will still be
>> needed.
>
> But it means that the implementation has to manage memory blocks based
> on size instead of something else - if it had to store anything else
> with the pointer, delegating the size to the programmer would be pointless

No, it does /not/ mean the implementation has to use the size. It means
it /can/ use the size - not that it has to!

And as I have said several times, and no one has (yet) given a
convincing counter-argument, the user code almost always knows the size
of the allocation already when it calls free. After all, it knows the
size when it allocates the memory, and it knows the size when it /uses/
the memory, so it's a tiny or non-existent cost to know the size when it
frees the memory.

>
> Moreover, the requested size (used by the user) may be different from
> the actual size (used by the allocator), which poses some consistency
> issues - but that's a side note.

The "free_untracked" implementation would match the "malloc_untracked"
algorithm - any changes made to the requested size (such as rounding up
to an allocation unit size) would be known to the "free" routine.

>
>>
>>> I believe there are countless implementations of malloc out there, and I
>>> guess this might not be the case, or it may be the case today but not
>>> tomorrow.
>>> The point is that if you set the interface to be pointer+size you also
>>> put constraints on the implementation, on top of the extra burden for
>>> the programmer.
>>>
>> How could this suggestion impose any constraints on the implementation?
>>   The interface for getting the memory is the same (albeit with a
>> different name, in order to keep compatibility with existing code), and
>> the interface for freeing memory gives the implementation more
>> information.  It would be perfectly possible (though a little unhelpful)
>> for an implementation of the "_untracked" functions to be :
>>
>>     #define malloc_untracked(size) malloc(size)
>>     #define free_untracked(ptr, size) free(ptr)
>>
>> An implementation can do better, given this new information, but there
>> are no new limitations or restrictions.
>>
>
> Well, requiring the programmer to keep track of the block size is a
> significant programming overhead - if it is unused, it's a significant
> waste I'd rather not pay for.

Does this mean you understand why this idea does not impose any
constraints on how the heap is implemented?

And can you give examples of where you have a pointer to a block of
memory but do not know the size of the memory?

Re: on why declare a struct with a single array in it

<snfv3u$qc1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19136&group=comp.lang.c#19136

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Mon, 22 Nov 2021 11:32:15 +0000
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <snfv3u$qc1$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<KvhmJ.55056$np6.2360@fx46.iad> <snditr$7l1$1@dont-email.me>
<sne3b7$1mre$1@gioia.aioe.org> <snebg4$jtk$1@dont-email.me>
<snf1gn$r3q$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 22 Nov 2021 11:32:14 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f7c8e0aaba4a88705ca4bfe292c42e7b";
logging-data="27009"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4ZoHqF4PAbQKKlnyrMWN4RnjSfwFQ0p0="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:UlE6xxDD24BBwNvo8V1cEtvqR/I=
In-Reply-To: <snf1gn$r3q$1@gioia.aioe.org>
 by: Bart - Mon, 22 Nov 2021 11:32 UTC

On 22/11/2021 03:06, Guillaume wrote:
> Le 21/11/2021 à 21:51, David Brown a écrit :
>> No.  It merely relieves the heap implementation from having to store the
>> size of the allocation.  Any other metadata (such as for tracking free
>> space lists, or handling efficient multi-threaded access) will still be
>> needed.
>>
>>> I believe there are countless implementations of malloc out there, and I
>>> guess this might not be the case, or it may be the case today but not
>>> tomorrow.
>
>
> Of all the typical implementations of malloc() and similar
> general-purpose allocators, I've always seen the same approach of
> storing metadata either right before (with then the appropriate padding
> if required), or right after the actual usable block of memory. In a
> number of cases, the metadata is even duplicated both before and after
> the block - for data locality reasons. This can be more efficient
> cache-wise.

Have you considered the reason why such implementations need to store
such metadata at all?

Namely, because the specification says that malloc must record the size
simply so that free() will know what it is!

Re: on why declare a struct with a single array in it

<sng0gm$kiq$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19137&group=comp.lang.c#19137

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Mon, 22 Nov 2021 11:56:06 +0000
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <sng0gm$kiq$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<KvhmJ.55056$np6.2360@fx46.iad> <snditr$7l1$1@dont-email.me>
<sne3b7$1mre$1@gioia.aioe.org> <snebg4$jtk$1@dont-email.me>
<sneuje$1qm$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Nov 2021 11:56:06 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f7c8e0aaba4a88705ca4bfe292c42e7b";
logging-data="21082"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/oF48i3tm6iIj3oF/OGv0nio8UJ0UJC98="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:8LWcX325Y2AAlm6tNNy2B5v3fHY=
In-Reply-To: <sneuje$1qm$1@gioia.aioe.org>
 by: Bart - Mon, 22 Nov 2021 11:56 UTC

On 22/11/2021 02:17, Manfred wrote:
> On 11/21/2021 9:51 PM, David Brown wrote:

>> An implementation can do better, given this new information, but there
>> are no new limitations or restrictions.
>>
>
> Well, requiring the programmer to keep track of the block size is a
> significant programming overhead
Why is it a significant overhead for user-code, but not for the malloc
implementation?!

In any case, I've already mentioned some situations where there is
pretty much zero extra overhead for the programmer:

* When allocating a fixed sized struct or other fixed size type

* When allocating a block where the programmers needs to record the size
anyway

Or the programmer can determine the size:

* When allocating a zero-terminated string (they can use strlen if not
recording the size)

> - if it is unused, it's a significant waste I'd rather not pay for.

Exactly - it is an overhead of malloc that I'd rather not pay for either!

Just so programmers can do this:

p[i] = malloc(rand()); // or some varying size

....

free(p[i]);

I see that as a luxury in a lower-level language.

And also, goes against the 'spirit' of C where you are supposed to have
precise control over such things.

Potentially doubling (or sometimes quadrupling) heap memory usage for no
good reason sort of goes against that.

Re: on why declare a struct with a single array in it

<87ee77j47i.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19142&group=comp.lang.c#19142

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Mon, 22 Nov 2021 13:00:33 -0800
Organization: None to speak of
Lines: 44
Message-ID: <87ee77j47i.fsf@nosuchdomain.example.com>
References: <86tughjjm7.fsf@levado.to> <87pmr5qih6.fsf@bsb.me.uk>
<smmhhg$bfh$1@dont-email.me> <sn5pcl$pru$1@dont-email.me>
<sn5qji$3ri$1@dont-email.me> <sn5rc0$1i81$1@gioia.aioe.org>
<sn5ukn$2ir$1@dont-email.me> <sn6iam$n5t$1@dont-email.me>
<sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com>
<Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com> <snc3a0$6gs$1@dont-email.me>
<KvhmJ.55056$np6.2360@fx46.iad> <snditr$7l1$1@dont-email.me>
<sne3b7$1mre$1@gioia.aioe.org> <snebg4$jtk$1@dont-email.me>
<snf1gn$r3q$1@gioia.aioe.org> <snfv3u$qc1$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="58c43985092162f95fdbb2eb9dbfb717";
logging-data="8872"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19lLi7JCx/sEffFPHW2eiu7"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:7UyRGrAcZwH8moIMZ/rDl0o+yKc=
sha1:odTOh6gwQ2Goph3rpGYW6bna4jg=
 by: Keith Thompson - Mon, 22 Nov 2021 21:00 UTC

Bart <bc@freeuk.com> writes:
> On 22/11/2021 03:06, Guillaume wrote:
>> Le 21/11/2021 à 21:51, David Brown a écrit :
>>> No.  It merely relieves the heap implementation from having to store the
>>> size of the allocation.  Any other metadata (such as for tracking free
>>> space lists, or handling efficient multi-threaded access) will still be
>>> needed.
>>>
>>>> I believe there are countless implementations of malloc out there, and I
>>>> guess this might not be the case, or it may be the case today but not
>>>> tomorrow.
>>
>> Of all the typical implementations of malloc() and similar
>> general-purpose allocators, I've always seen the same approach of
>> storing metadata either right before (with then the appropriate
>> padding if required), or right after the actual usable block of
>> memory. In a number of cases, the metadata is even duplicated both
>> before and after the block - for data locality reasons. This can be
>> more efficient cache-wise.
>
> Have you considered the reason why such implementations need to store
> such metadata at all?
>
> Namely, because the specification says that malloc must record the
> size simply so that free() will know what it is!

There's no explicit requirement for free() to know the size of the
record being deallocated. It likely will in most implementations,
but I can imagine an implementation where deallocation doesn't
require knowing the size.

One example: malloc() may allocate more size than was asked for, and
remember only the actual allocated size, not the requested size.
Or the allocated size might change after allocation if a following
block is freed. (But realloc() does make it convenient to have some
idea of what was requested.)

Or malloc() and free() might be implemented on top of some lower
level interface that doesn't expose that information.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Pages:123
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor