Why are immutable array literals heap allocated?

Nick Treleaven nick at geany.org
Thu Jul 4 10:56:50 UTC 2019


immutable(int[]) f() @nogc {
     return [1,2];
}

onlineapp.d(2): Error: array literal in `@nogc` function 
`onlineapp.f` may cause a GC allocation

This makes dynamic array literals unusable with @nogc, and adds 
to GC pressure for no reason. What code would break if dmd used 
only static data for [1,2]?


More information about the Digitalmars-d-learn mailing list