Do array literals still always allocate?
Nicholas Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat May 13 16:57:17 PDT 2017
On Saturday, 13 May 2017 at 18:32:16 UTC, Lewis wrote:
> import std.random;
> import std.stdio;
>
> int[4] testfunc(int num) @nogc
> {
> return [0, 1, num, 3];
> }
>
> int main()
> {
> int[4] arr = testfunc(uniform(0, 15));
> writeln(arr);
> return 0;
> }
>
> I've read a bunch of stuff that seems to indicate that array
> literals are always heap-allocated, even when being used to
> populate a static array. However, testfunc() above compiles as
> @nogc. This would indicate to me that D does the smart thing
> and avoids a heap allocation for an array literal being used to
> populate a static array. Is all the old stuff I was reading
> just out-of-date now?
1D arrays it doesn't, 2D or higher it does.
More information about the Digitalmars-d-learn
mailing list