Can't understand if deallocation happens?

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 22 05:13:06 PST 2017


On Sunday, 22 January 2017 at 12:49:11 UTC, Suliman wrote:
> import std.stdio;
> import std.string;
> import core.memory;
>
> void main()
> {
>     char [] str = "aaa".dup;
>     char [] *str_ptr = &str;
>
>     writeln("before: ", str_ptr.ptr);// address of structure
>     writeln(*str_ptr.ptr); // address of data
>     str[] = "bbb"[]; // now writing to structure new data, so 
> ptr would be point to them
>     writeln("after: ", str_ptr.ptr);
>     writeln("length: ", str_ptr.length);
>     writeln("str_ptr point to: ", *str_ptr.ptr);
>
>     writeln(str_ptr);
> 	
>     writeln("before dealloc: ", str_ptr.length);
>     GC.free(str_ptr.ptr);
>     writeln(str_ptr);
>     writeln("after dealloc: ", str_ptr.length);
>
> }
>
> As I understand str structure would have 2 data peace after 
> writing "bbb" to it. But ptr will point to last one (to "bbb").
>
> [aaa] [bbb]
> --------+
>
> So after appending

You do not append to anything, only overwrite it. There is no 
reallocation because
"aaa".length == "bbb".length.

> "bbb" I should see that the size is 3, after appending 6. After 
> GC again 3. But I see only 3 before and after GC:
>
>> app.exe
> before: 2641010
> a
> after: 2641010
> length: 3
> str_ptr point to: b
> 19FE0C
> before dealloc: 3
> 19FE0C
> after dealloc: 3
>
> And am I right understand that here memory should be GC-ed?
>
> Another question:
> writeln("before: ", str_ptr.ptr);// address of structure
>
> Is it's address from 0 of current App memory?




More information about the Digitalmars-d-learn mailing list