Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: wij Newsgroups: comp.lang.c Subject: Re: What is your opinion about init_malloc? Date: Fri, 14 Mar 2025 23:56:30 +0800 Organization: A noiseless patient Spider Lines: 47 Message-ID: <6255bb4153bcc571db879cf847ee3a110080c4fc.camel@gmail.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Date: Fri, 14 Mar 2025 16:56:31 +0100 (CET) Injection-Info: dont-email.me; posting-host="a499a0831bd7f6466cd8a05ed4720a40"; logging-data="1693018"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+l4m32/QXL77a6wBTzAi1E" User-Agent: Evolution 3.54.3 (3.54.3-1.fc41) Cancel-Lock: sha1:uWemfBXDCQwKZ2M2zvC/tKrCjFE= In-Reply-To: Bytes: 2437 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. > >=20 > > #include > > #include > > #include > >=20 > > void * init_malloc(size_t size, void * src) > > { > > =C2=A0=C2=A0 void * p =3D malloc(size); > > =C2=A0=C2=A0 if (p) { > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 memcpy(p, src, size ); > > =C2=A0=C2=A0 } > > =C2=A0=C2=A0 return p; > > } > >=20 > > #define ALLOC(OBJ) ((typeof(OBJ)*) init_malloc(sizeof(OBJ), &(OBJ))) > >=20 > > ////////// SAMPLE=C2=A0 ////////// > >=20 > > struct Mail { > > =C2=A0=C2=A0=C2=A0=C2=A0 const int id; > > }; > >=20 > > int main () { > > =C2=A0=C2=A0=C2=A0=C2=A0 struct Mail* p0 =3D ALLOC((struct Mail){.id=3D= 1}); > >=20 > > =C2=A0=C2=A0=C2=A0=C2=A0 struct Mail* p1 =3D init_malloc(sizeof *p1, &(= struct Mail){.id=3D 1}); > >=20 > > =C2=A0=C2=A0=C2=A0=C2=A0 auto=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 p2 =3D ALLOC((struct Mail){.id=3D 1}); > >=20 > > } > >=20 > > (I also posted on reddit) > >=20 >=20 > C still seems to be a ridiculous language. >=20 If it tries to exceed 'High level assembly', it could be ridiculous. Same as C++, if it tries to be 'not-C'.