Allocating structs with new?

bearophile bearophileHUGS at lycos.com
Sun Jul 11 11:38:35 PDT 2010


Jonathan M Davis:
> It never occurred to me to put a struct on the heap. Obviously, it's not a 
> feature that I use often.

Structs are simpler for the compiler, help you produce a smaller program, require less memory for each instance (two words less, not counting the chunk sizez allocated by the GC), the runtime has a bit less things to do to manage them. Putting structs on the heap can be useful if you want to build certain efficient data structures, like certain trees, but you may want to allocate many of them at the same time anyway. One advantage of structs is that they are simpler, so if you have to do simpler things, using a struct you denote that you are doing something simple in your code :-) There's no risk of having virtual functions or inheritance etc if you use structs.

Bye,
bearophile


More information about the Digitalmars-d mailing list