ImportC with Garbage Collector

Valthorn valthorn at outlook.com
Thu Apr 4 09:52:14 UTC 2024


Hello, today I wrote the following code to try out ImportC:

```c
// functions.c
#include <stdio.h>
#include <stdlib.h>

void allocateMemory() {
     int *data = (int *)malloc(1024 * 1024 * sizeof(int));

     if (data == NULL) {
         fprintf(stderr, "Memory could not be allocated.\n");
         exit(EXIT_FAILURE);
     }

     printf("4MB of memory allocated.\n");
}
```

```d
// demo.d
import std.stdio;
import core.memory;
import functions;

void main() {
     writeln("Before allocating memory. Memory: ", 
GC.stats().usedSize, " bytes");
     allocateMemory();
     writeln("After allocating memory. Memory: ", 
GC.stats().usedSize, " bytes");

     GC.collect();
     writeln("GC.collect() was called. Memory: ", 
GC.stats().usedSize, " bytes");
}
```

Here I tried to check the operation of GC with ImportC, but 
there's a situation I'm curious about. How exactly does ImportC 
work? Can't it be used together with GC? Might there be an 
ImportC update in the future that will also encompass the GC?


More information about the Digitalmars-d mailing list