Can't understand if deallocation happens?
Suliman via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jan 22 06:45:57 PST 2017
> Supported answer: you don't, it has infinite lifetime and
> you're claiming it is immutable, but then trying to pull the
> memory out from under it! The supported solution is simply to
> let the garbage collector manage it.
>
> But..
>
>> //GC.free(str_ptr.ptr); // Error: function
>> core.memory.GC.free (void* p) is not callable using argument
>> types (immutable(char)*)
>
>
> If you must call the function though, you can simply
> `cast(void*) str.ptr`, or use `char[]` up above instead of
> `string` when declaring it, so it has a mutable pointer to
> begin with. Since you are using .dup anyway, both declarations
> are equally legal.
string str = "aaa";
writeln(str.length); // print 3
str = "bbbb";
writeln(str.length); //print 4 expected 3+4=7 [aaa][bbbb] (2
blocks)
GC.free(cast(void*) str.ptr);
writeln(str.length); //print 4
More information about the Digitalmars-d-learn
mailing list