🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2251

Re: is wraper on realloc good idea? maybe more wrappers?

#2251
From: Ross Finlayson
Date: Sat, 05 Dec 2026 15:18
76 lines
4339 bytes
On 12/05/2026 04:59 AM, Ross Finlayson wrote:
> On 12/05/2026 04:38 AM, Ross Finlayson wrote:
>> On 12/05/2026 12:10 AM, Tristan Wibberley wrote:
>>> On 29/11/2026 12:16, fir wrote:
>>>> never used it but generally wrappers on some standard functions
>>>> probably are good idea, or not?
>>>
>>> The art of programming is all about wrapping, wrapping all the way down.
>>>
>>> The compsci way is to write a solution in the language of the problem
>>> domain then interpret that language in a language of the domain of
>>> interpreting that (class of) languages, and so forth until you get to
>>> your preferred system programming language. That's all wrapping and it's
>>> equivalent to looping and recursion without wrapping, just an
>>> alternative expression.
>>>
>>> But it's mostly a waste of time to wrap for the sake of wrapping -
>>> unless you're adding some type annotations/(pre/post)conditions/asserts,
>>> etc. Sometimes you can use the linker for that stuff instead of C.
>>>
>>> I don't know where to place EDSLs there.
>>>
>>> In C++ I sometimes wrap heap-stuff with smart-references because
>>> managing heap allocations is too hard, like chess - the rules are easy
>>> but it's not so easy to play a game; that's /almost/ an EDSL: couching
>>> allocation in terms of lexical scopes.
>>>
>>
>> Often usual accounts of low-level programming in C/C++ like C++ 98/03
>> have that reference-counting smart-pointers hand-written in a page or
>> two are entirely common and completely ubiquitous for heap objects.
>>
>>
>
> In many C/C++ projects of that time, the only reason to use C++ at
> all was reference-counting smart-pointers for heap-objects, that
> for the account of "defer" in C making for grouping together in
> statement the effects of block & function scoping their exit, one
> of the very most usual sorts of things would be reference-counting
> smart-pointers.
>
>


typedef struct {
   void* ptr;
   size_t ref_count;
} spa; /* "smart-pointer automatic" */

spa spa_ref(spa* sp) {  sp->ref_count++; return *sp; }
void spa_def(spa* sp) { if (sp->ref_count-- == 1) free(sp->ptr); }



spa f() {

   spa sp = {malloc(123);}; defer spa_def(&sp);

   bzero(sp.ptr, 123);

   return spa_ref(&sp);

}

Since spa is a struct returned by value, then it doesn't need
heap-management itself, "trivially copyable" and "trivially
relocatable", just remembering that passing it by value
results copies not references/pointers, then that anything
calling malloc must call free, yet it can be on the same line
with defer taking care of cleaning up block/function scope.


Then about hiding the allocator while exposing the type,
or making it generic yet specialized, has various approaches.

Technical Details
Message-ID: <4vCdnQFPTN8eton2nZ2dnZfqnPGdnZ2d@giganews.com>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
References: <11eh538$3fet7$1@dont-email.me> <11f0gtg$2i7jn$3@dont-email.me> <YtKcnd7kZMAsmIn2nZ2dnZfqnPadnZ2d@giganews.com> <WtednTb8ptlWl4n2nZ2dnZfqnPYk4z6o@giganews.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Sat, 05 Dec 2026 15:19:31 +0000
Subject: Re: is wraper on realloc good idea? maybe more wrappers?
Newsgroups: comp.lang.c
References: <11eh538$3fet7$1@dont-email.me> <11f0gtg$2i7jn$3@dont-email.me>
 <YtKcnd7kZMAsmIn2nZ2dnZfqnPadnZ2d@giganews.com>
 <WtednTb8ptlWl4n2nZ2dnZfqnPYk4z6o@giganews.com>
From: Ross Finlayson <ross.a.finlayson@gmail.com>
Date: Sat, 5 Dec 2026 07:18:58 -0800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101
 Thunderbird/38.6.0
MIME-Version: 1.0
In-Reply-To: <WtednTb8ptlWl4n2nZ2dnZfqnPYk4z6o@giganews.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <4vCdnQFPTN8eton2nZ2dnZfqnPGdnZ2d@giganews.com>
Lines: 76
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-RbKd9ComoGojjbz/gOowLPqYDMYBb+8flebI3cbjuwFsHJePwfmHy+AKLYWLTo38O0Edi2dqY3DyMic!9x/jqanle41d7xwpakxr1Pj/Mvx8X10immkr8vbeooWgDYvq3BlXTIQoKg2d/afMkzG1l7HOra04!ow==
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40