Sujet : Re: What is your opinion about init_malloc?
De : wyniijj5 (at) *nospam* gmail.com (wij)
Groupes : comp.lang.cDate : 14. Mar 2025, 16:56:30
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <6255bb4153bcc571db879cf847ee3a110080c4fc.camel@gmail.com>
References : 1 2
User-Agent : Evolution 3.54.3 (3.54.3-1.fc41)
On Fri, 2025-03-14 at 16:49 +0100, Bonita Montero wrote:
Am 14.03.2025 um 15:24 schrieb Thiago Adams:
What is your opinion about init_malloc?
One problem it solves it to initialise a const objects on heap.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void * init_malloc(size_t size, void * src)
{
void * p = malloc(size);
if (p) {
memcpy(p, src, size );
}
return p;
}
#define ALLOC(OBJ) ((typeof(OBJ)*) init_malloc(sizeof(OBJ), &(OBJ)))
////////// SAMPLE //////////
struct Mail {
const int id;
};
int main () {
struct Mail* p0 = ALLOC((struct Mail){.id= 1});
struct Mail* p1 = init_malloc(sizeof *p1, &(struct Mail){.id= 1});
auto p2 = ALLOC((struct Mail){.id= 1});
}
(I also posted on reddit)
C still seems to be a ridiculous language.
If it tries to exceed 'High level assembly', it could be ridiculous.
Same as C++, if it tries to be 'not-C'.