Search code examples
d

What is the difference between core.stdc.stdlib.malloc and core.memory.GC.malloc?


What is the difference between core.stdc.stdlib.malloc and core.memory.GC.malloc? Which of these should I use when a program runs with GC disabled?


Solution

  • core.stdc.stdlib.malloc is plain C's malloc, so memory is not registred to GC. This means it will not be scanned and you must use C's free to free this memory. core.memory.GC.malloc is registred by GC and it will be scanned. You can use both of them. But if you use GC.disable even memory alloceted by core.memory.GC.malloc would not be freed until you enable GC back, or you call GC.collect.

    Btw. if you want better control over memory allocation you should look at https://dlang.org/phobos/std_experimental_allocator.html