As soon as the memory referenced by a isn't referenced anymore through any variable in the program, the garbage collector will free its memory.
D also allows pointer arithmetic, except in code that is marked as @safe
.
void safeFun() @safe
{
writeln("Hello World");
// allocating memory with the GC is safe too
int* p = new int;
}
void unsafeFun()
{
int* p = new int;
int* fiddling = p + 5;
}
void main()
{
safeFun();
unsafeFun();
}
For more information about SafeD see the article from the D design team.