lame question

Francisco Almeida francisco.m.almeida at gmail.com
Mon Apr 18 02:25:55 PDT 2011


== Quote from %u (lenochware at gmail.com)'s article
> Is it necessary free memory allocated for member of structure, like in C? I
> suppose not (we have gc). Example:
> struct BITMAP {
> (...)
> ubyte[] pixels;
> }
> BITMAP* bitmap = new BITMAP;
> bitmap.pixels = new ubyte[100*100];
> (...)
> // delete bitmap.pixels; //not necessary?
> delete bitmap;

Using new implies that the allocated memory will be managed by the GC. You
shouldn't need to delete any pointers.

Note, however, that the availability of GC does not exclude manual memory
management. Especially when you use structs, which are preferably allocated on the
stack. D supports RAII as well - with struct.

If you want to include heap management in the resources allocation of BITMAP, it
is advisable to use C-style malloc() and free() in the constructor and destructor.



More information about the Digitalmars-d mailing list