Tutorial by Examples

D is a system programming language and thus allows you to manually manage and mess up your memory. Nevertheless, D uses a garbage collector per default to free unused memory. D provides pointer types T* like in C: void main() { int a; int* b = &a; // b contains address of a aut...
A new memory block on the heap is allocated using the new expression, which returns a pointer to the managed memory: void main() { int* a = new int; *a = 42; // dereferencing import std.stdio : writeln; writeln("a: ", *a); }
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"); // allocatin...

Page 1 of 1