View source code
Display the source code in core/memory.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function core.memory.pureMalloc

Pure variants of C's memory allocation functions malloc, calloc, and realloc and deallocation function free.

void* pureMalloc (
  size_t size
) pure nothrow @nogc @trusted;

UNIX 98 requires that errno be set to ENOMEM upon failure. Purity is achieved by saving and restoring the value of errno, thus behaving as if it were never changed.

See Also

D's rules for purity, which allow for memory allocation under specific circumstances.

Example

ubyte[] fun(size_t n) pure
{
    void* p = pureMalloc(n);
    p !is null || n == 0 || assert(0);
    scope(failure) p = pureRealloc(p, 0);
    p = pureRealloc(p, n *= 2);
    p !is null || n == 0 || assert(0);
    return cast(ubyte[]) p[0 .. n];
}

auto buf = fun(100);
writeln(buf.length); // 200
pureFree(buf.ptr);

Example

Deleting classes

bool dtorCalled;
class B
{
    int test;
    ~this()
    {
        dtorCalled = true;
    }
}
B b = new B();
B a = b;
b.test = 10;

assert(GC.addrOf(cast(void*) b) != null);
__delete(b);
assert(b is null);
assert(dtorCalled);
writeln(GC.addrOf(cast(void*)b)); // null
// but be careful, a still points to it
assert(a !is null);
assert(GC.addrOf(cast(void*) a) == null); // but not a valid GC pointer

Example

Deleting interfaces

bool dtorCalled;
interface A
{
    int quack();
}
class B : A
{
    int a;
    int quack()
    {
        a++;
        return a;
    }
    ~this()
    {
        dtorCalled = true;
    }
}
A a = new B();
a.quack();

assert(GC.addrOf(cast(void*) a) != null);
__delete(a);
assert(a is null);
assert(dtorCalled);
writeln(GC.addrOf(cast(void*)a)); // null

Example

Deleting structs

bool dtorCalled;
struct A
{
    string test;
    ~this()
    {
        dtorCalled = true;
    }
}
auto a = new A("foo");

assert(GC.addrOf(cast(void*) a) != null);
__delete(a);
assert(a is null);
assert(dtorCalled);
writeln(GC.addrOf(cast(void*)a)); // null

Example

Deleting arrays

int[] a = [1, 2, 3];
auto b = a;

assert(GC.addrOf(b.ptr) != null);
__delete(b);
assert(b is null);
writeln(GC.addrOf(b.ptr)); // null
// but be careful, a still points to it
assert(a !is null);
assert(GC.addrOf(a.ptr) == null); // but not a valid GC pointer

Example

Deleting arrays of structs

int dtorCalled;
struct A
{
    int a;
    ~this()
    {
        writeln(dtorCalled); // a
        dtorCalled++;
    }
}
auto arr = [A(1), A(2), A(3)];
arr[0].a = 2;
arr[1].a = 1;
arr[2].a = 0;

assert(GC.addrOf(arr.ptr) != null);
__delete(arr);
writeln(dtorCalled); // 3
writeln(GC.addrOf(arr.ptr)); // null

Authors

Sean Kelly, Alex Rønne Petersen

License

Boost License 1.0