Dynamic array ot not

Steven Schveighoffer schveiguy at gmail.com
Mon Jan 17 03:11:50 UTC 2022


On 1/16/22 9:42 PM, forkit wrote:
> On Monday, 17 January 2022 at 00:54:19 UTC, forkit wrote:
>      // mArr2 is a dynamic array allocated on the gc heap
>      // although compiling with '-profile=gc' incorrectly suggests otherwise.


> 
> if add @nogc to above code:
> (8): Error: array literal in `@nogc` function `D main` may cause a GC 
> allocation
> (22): Error: `@nogc` function `D main` cannot call non- at nogc function 
> `std.array.array!(MapResult!(array, Chunks!(Result))).array`

Technically, this is using 2 different mechanisms.

@nogc is a function attribute that means it can't call other functions 
not marked as @nogc.

This is actually a *compile-time* mechanism. A call to `array` may not 
actually allocate, but *might* allocate, and therefore it's statically 
not allowed to be called from a @nogc function.

The profile=gc appears to only show GC allocations that the *compiler* 
initiates (i.e. via `new`, array operations (like appending) or closure 
allocations). It does not detect that the functions that actually 
allocate memory themselves (such as `core.memory.GC.malloc`) are GC 
allocations. This actually does not care whether a function might or 
might not allocate, but records when it actually does allocate.

-Steve


More information about the Digitalmars-d-learn mailing list