Sujet : Re: What is your opinion about init_malloc?
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 15. Mar 2025, 17:11:36
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86pliiqnnr.fsf@linuxsc.com>
References : 1 2
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Kaz Kylheku <
643-408-1753@kylheku.com> writes:
On 2025-03-14, Thiago Adams <thiago.adams@gmail.com> 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().