Question about @nogc in D 2.066

Timon Gehr via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 11 14:02:30 PDT 2014


On 07/11/2014 10:39 PM, Dicebot wrote:
> Key difference is that type of string literal is immutable(char)[] so it
> is perfectly legal to keep it in binary text segment. Type of array
> literal is just T[] (int[] here) and you can possibly mutate their
> elements. Because of this each assignment of array literal needs to
> allocate a new copy contrary to immutable strings which can all
> reference same memory chunk.

He is allocating an immutable(int)[] here. There is no reason why it 
should allocate unless providing different addresses for different runs 
of the code is considered a feature, as the literal has a compile-time 
known value. It's just that the frontend does not actually elide the 
allocation, and this would be a valid extension to ask for.

In any case, the allocation can be manually elided as follows:

void main(string[] args)@nogc{
     auto s1 = "hello";
     static immutable _a1 = [1, 2];
     auto a1 = _a1;
}

(Not actually tested, because I am having trouble downloading the beta 
for some reason.)


More information about the Digitalmars-d-learn mailing list