Allocating Memory

Get acquainted with the memory allocation methods in D.

We'll cover the following...

Memory allocation methods

System languages allow programmers to specify the memory areas where objects should live. Such memory areas are commonly called buffers.

There are several methods of allocating memory. The simplest method is using a fixed-length array:

ubyte[100] buffer;    // A memory area of 100 bytes

buffer is ready to be used as a 100-byte memory area. Instead of ubyte, it is also possible to define such buffers as arrays of void, without any association to any type. Since void cannot be assigned any value, it cannot have the .init value either. Such arrays must be initialized by the special syntax =void:

void[100] buffer = void; // A memory area of 100 bytes

We will use only GC.calloc from the core.memory module to reserve memory. That module has many other features that are useful in various situations.

Additionally, the memory allocation functions of the C standard library are available in the core.stdc.stdlib module. GC.calloc allocates a memory area of the specified size pre-filled with all 0 values, and ...