Bottom line re GC in D

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 8 04:22:40 PDT 2014


On Tuesday, 8 July 2014 at 06:23:13 UTC, Jeremy DeHaan wrote:

> I remember that slices was one thing you would no longer have 
> if you disable the GC, but I can't think of any others.

You can definitely use slices without the GC and they are still 
awesome without the GC.
What you cannot do is create them with `new` or call the builtin 
~ or ~= (concatenate and append respectively) operators on 
slices. Slice literals may also cause allocations, but not always:

enum ctA = [3, 4];

void main() @nogc //@nogc is transitive, marking main as @nogc
                   //enforces no GC activity in the entire program.
{
     //these statements are all guaranteed not to GC-allocate
     int[2] a = [1, 2];
     assert(a[0] == 1 && a[1] == 2);
     a = ctA;
     assert(a[0] == 3 && a[1] == 4);
     assert(a == [3,4]);

     import core.stdc.stdlib;
     auto data = cast(int*)calloc(100_000, int.sizeof);
     auto callocedSlice = data[0 .. 100_000];
     auto subSlice = callocedSlice[40 .. $ - 600];

     //these cause GC allocation and as such
     //will not compile in an @nogc function.
//    int[] s0 = [1, 2];
//    auto s1 = [3,4];
}


More information about the Digitalmars-d mailing list