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.pureCalloc
Pure variants of C's memory allocation functions malloc, calloc, and
 realloc and deallocation function free.
						
				void* pureCalloc
				(
				
				  size_t nmemb,
				
				  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(bufExample
Deleting classes
bool dtorCalled;
class B
{
    int test;
    ~this()
    {
        dtorCalled = true;
    }
}
B b = new B();
B a = b;
bExample
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();
aExample
Deleting structs
bool dtorCalled;
struct A
{
    string test;
    ~this()
    {
        dtorCalled = true;
    }
}
auto a = new A("foo");
assert(GCExample
Deleting arrays
int[] a = [1, 2, 3];
auto b = a;
assert(GCExample
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]Authors
Sean Kelly, Alex Rønne Petersen
License
					Copyright © 1999-2024 by the D Language Foundation | Page generated by ddox.