Bottom line re GC in D

Oluca via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 8 04:31:48 PDT 2014


On Tuesday, 8 July 2014 at 11:26:55 UTC, John Colvin wrote:
> On Tuesday, 8 July 2014 at 11:22:42 UTC, John Colvin wrote:
>> 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];
>> }
>
> I should also include this example:
>
> int[10] a = [0,1,2,3,4,5,6,7,8,9]; //a static array, on the 
> stack
> auto s = a[]; //a normal slice, backed by stack memory*
> auto s1 = a[3 .. 5]; //ditto
I see. Thanks for the examples. What about strings? Do they 
depend on GC?


More information about the Digitalmars-d mailing list