str_ptr.ptr returns exactly the same thing as str.ptr or (*str_ptr).ptr, a pointer to the contents. When you write str_ptr, you print the pointer to the container.

So str.ptr is just shortcut?

Ok, but how to free memory from first located value (from `aaa`)?

I changed my code to next:

import std.stdio;
import std.string;
import core.memory;

void main()
{
    string str = "aaa".dup;
    string *str_ptr = &str;

    writeln("before: ", str_ptr.ptr);// address of structure
    writeln(*str_ptr.ptr); // address of data
str = "bbbb"; // 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); // Error: function core.memory.GC.free (void* p) is not callable using argument types (immutable(char)*)
    writeln(str_ptr);
    writeln("after dealloc: ", str_ptr.length);

}

But I can't call `GC.free(str_ptr.ptr)` on string type, only on `char []`

Reply via email to