Sujet : Re: What is your opinion about init_malloc?
De : rjh (at) *nospam* cpax.org.uk (Richard Heathfield)
Groupes : comp.lang.cDate : 14. Mar 2025, 17:50:18
Autres entêtes
Organisation : Fix this later
Message-ID : <vr1moa$1kn9g$2@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 14/03/2025 16:31, wij wrote:
On Fri, 2025-03-14 at 17:13 +0100, Bonita Montero wrote:
Am 14.03.2025 um 16:56 schrieb wij:
>
If it tries to exceed 'High level assembly', it could be ridiculous.
Same as C++, if it tries to be 'not-C'.
>
Thiago wants to mimic construction with new,
I've been wondering how C would mimic ctor/dtor for long long time,...
void * init_malloc(size_t size, void * src) looked reasonable, and
may be necessary in 'my assembly model'.
Why not pass it a constructor function as well? Something like:
void * init_malloc(void *src,
size_t size,
int(*ctor)(void *, const void *))
{
void *p = malloc(size);
if(p)
{
if(ctor != NULL)
{
if((*ctor)(p, src) == CTOR_FAIL)
{
free(p);
p = NULL;
}
}
else
{
memcpy(p, src, size);
}
}
return p;
}
This way, you can do a "deep copy" instead of a naive bit blit, and one init_malloc fits all because you just write a new constructor for each type that needs one and pass a NULL constructor when a bit blit suffices.
-- Richard HeathfieldEmail: rjh at cpax dot org dot uk"Usenet is a strange place" - dmr 29 July 1999Sig line 4 vacant - apply within