How to use D without the GC ?
    bachmeier 
    no at spam.net
       
    Wed Jun 12 15:33:39 UTC 2024
    
    
  
A SafeRefCounted example with main marked @nogc:
```
import std;
import core.stdc.stdlib;
struct Foo {
   double[] data;
   double * ptr;
   alias data this;
   @nogc this(int n) {
     ptr = cast(double*) malloc(n*double.sizeof);
     data = ptr[0..n];
     printf("Data has been allocated\n");
   }
   @nogc ~this() {
     free(ptr);
     printf("Data has been freed\n");
   }
}
@nogc void main() {
   auto foo = SafeRefCounted!Foo(3);
   foo[0..3] = 1.5;
   printf("%f %f %f\n", foo[0], foo[1], foo[2]);
}
```
    
    
More information about the Digitalmars-d-learn
mailing list