Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: What is your opinion about init_malloc? Date: Sat, 15 Mar 2025 09:11:36 -0700 Organization: A noiseless patient Spider Lines: 25 Message-ID: <86pliiqnnr.fsf@linuxsc.com> References: <20250314123035.751@kylheku.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Sat, 15 Mar 2025 17:11:36 +0100 (CET) Injection-Info: dont-email.me; posting-host="3e46727b472ba2d5903adccba45d2adf"; logging-data="3913750"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18UT85y6QUdAr1CnvzRNtCFbfsp5M+Lb0Y=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:Tq74sGeQOv/Bp/MydeSVDz8Ke3I= sha1:vyKZp1ByxP7dJoSSZUrrDByX9EU= Xref: csiph.com comp.lang.c:391242 Kaz Kylheku <643-408-1753@kylheku.com> writes: > On 2025-03-14, Thiago Adams wrote: > >> What is your opinion about init_malloc? >> One problem it solves it to initialise a const objects on heap. > > I don't think it's that useful, because I would rather write a > type-specific allocating constructor: > > struct Mail *Mail_alloc(int id) { /* ... you know what goes here ... */ } This idea is exactly backwards. It means writing a constructor function for every different type, and most types don't need one. For those types that would benefit from a type-specific constructor, it's easy to fabricate such functions on top of a single underlying allocation scheme. > No games with macros and struct literals that we hope the > compiler optimizes away. Constructors for structs are likely to use compound literals internally anyway. There is very little overhead to "optimize away", and beside that the overhead is likely to be negligible in comparison to the cost of calling malloc().