www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Overload new and delete to not use GC?

reply Rusty <me rustyx.org> writes:
I know it's possible to do [explicit object 
allocation](http://wiki.dlang.org/Memory_Management#Explicit_Class_
nstance_Allocation) on the heap, but I find that quite cumbersome.

So.. is it possible to overload 'new' and 'delete' to not use GC?

Also, it seems many features of the language rely on GC. Is there 
a definitive list of those?
May 22 2016
next sibling parent Guillaume Piolat <first.last gmail.com> writes:
On Sunday, 22 May 2016 at 07:35:32 UTC, Rusty wrote:
 I know it's possible to do [explicit object 
 allocation](http://wiki.dlang.org/Memory_Management#Explicit_Class_
nstance_Allocation) on the heap, but I find that quite cumbersome.

 So.. is it possible to overload 'new' and 'delete' to not use 
 GC?
The way to do it is to use emplace() and destroy() instead. https://p0nce.github.io/d-idioms/#Placement-new-with-emplace Unique!T, RefCounted!T, and Scoped!T use emplace() internally.
 Also, it seems many features of the language rely on GC. Is 
 there a definitive list of those?
- new - Appending and concatenating slices - Homogeneous template parameters void f( - Some array literals - Closures that escape - other features I don't recall nogc lets you avoid all of them and is necessary to avoid unintentional allocations.
May 22 2016
prev sibling parent Basile B. <b2.temp gmx.com> writes:
On Sunday, 22 May 2016 at 07:35:32 UTC, Rusty wrote:
 I know it's possible to do [explicit object 
 allocation](http://wiki.dlang.org/Memory_Management#Explicit_Class_
nstance_Allocation) on the heap, but I find that quite cumbersome.

 So.. is it possible to overload 'new' and 'delete' to not use 
 GC?
use the Mallocator with make and dispose: import std.stdio; import std.experimental.allocator: make, dispose; import std.experimental.allocator.mallocator: Mallocator; class Foo{} void main(string[] args) { Foo foo = make!Foo(Mallocator.instance); dispose(Mallocator.instance, foo); }
 Also, it seems many features of the language rely on GC. Is 
 there a definitive list of those ?
If you use DMD as compiler the switch -vgc can help to track GC allocations.
May 22 2016