How to allocate/free memory under @nogc

data pulverizer data.pulverizer at gmail.com
Thu May 21 02:50:22 UTC 2020


I'm a bit puzzled about the whole `@nogc` thing. At first I 
thought it just switched off the garbage collector and that you 
had to allocate and free memory manually using `import 
core.memory: GC;` I tried this and it failed because @nogc means 
that the function can not allocate/free memory 
(https://dlang.org/spec/function.html#nogc-functions). If this is 
the case, how do you allocate/free memory without using the 
garbage collector? Does allocating and freeing memory using 
`GC.malloc` and `GC.free` avoid D's garbage collector? Also what 
is the syntax for removing dangling pointers?

Can you also confirm that `@nogc` in a class do the same thing in 
that class as it does for a function? Also structs are not 
supposed to be tracked by the garbage collector but if I do 
something like this:

```
struct MyType(T)
{
   T[] data;
   this(size_t n)
   {
     data = new T[n];
   }
}
```

Is `data` in `MyType` tracked by the garbage collector? If it is 
how do I allocate it in such a way that it is not?

Thanks



More information about the Digitalmars-d-learn mailing list