allocate array with new

Jonathan M Davis jmdavisProg at gmx.com
Tue May 15 02:43:45 PDT 2012


On Tuesday, May 15, 2012 11:26:49 Namespace wrote:
> On Tuesday, 15 May 2012 at 09:23:51 UTC, Kagamin wrote:
> > Difference with what?
> > new is a safe feature: it allocates in the GC heap
> 
> That's what i mean. So i have to delete it yourself with "delete
> arr;", or not?

No. _Never_ use delete. It's going to be deprecated. The GC worries about 
freeing memory allocated on the GC heap, and new always allocates on the GC 
heap. If you don't want to allocate on the GC heap, then use malloc and free, 
in which case you _do_ need worry about freeing the memory.

If you need to force destruction before the GC collects an object, you can 
call clear on that object to have its destructor called and its vtbl zeroed 
out, but it's memory still isn't freed. That's the GC's job.

If you really have to, you can use core.memory to manipulate the GC heap 
(including calling GC.free), but you really shouldn't be messing with any of 
that unless you really need to and you know what you're doing.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list