On Monday, 23 January 2017 at 06:42:00 UTC, Suliman wrote:
You have *two* distinct strings here.
Yes, I understand, I am trying to find out how it's work on low
level. Any ideas why zero is used?
string *literals* in d are nul terminated to ease interoperation
with C
so
string s = "foo";
You have *two* distinct strings here.
Yes, I understand, I am trying to find out how it's work on low
level. Any ideas why zero is used?
On Sunday, 22 January 2017 at 15:59:47 UTC, Suliman wrote:
On Sunday, 22 January 2017 at 15:51:01 UTC, Suliman wrote:
string str = "abc";
writeln(str.ptr);
str = "def";
writeln("last data: ", *(str.ptr));
writeln("old data: ", *(str.ptr-1)); // print
On Sunday, 22 January 2017 at 15:51:01 UTC, Suliman wrote:
string str = "abc";
writeln(str.ptr);
str = "def";
writeln("last data: ", *(str.ptr));
writeln("old data: ", *(str.ptr-1)); // print nothing
writeln("old data: ", *(str.ptr-2)); // print c
string str = "abc";
writeln(str.ptr);
str = "def";
writeln("last data: ", *(str.ptr));
writeln("old data: ", *(str.ptr-1)); // print nothing
writeln("old data: ", *(str.ptr-2)); // print c
It's look like that there is some gap between data, because
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
On Sunday, 22 January 2017 at 14:04:55 UTC, Suliman wrote:
So str.ptr is just shortcut?
str.ptr is the actual member. In D, pointers to structs (and an
array is virtually the same as a struct) will automatically
dereference themselves.
T* t;
t.member; // automatically rewritten into
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:
On Sunday, 22 January 2017 at 12:49:11 UTC, Suliman wrote:
writeln(str_ptr);
writeln("before dealloc: ", str_ptr.length);
GC.free(str_ptr.ptr);
writeln(str_ptr);
You freed the CONTENTS, but are printing the CONTAINER.
str_ptr.ptr returns exactly the same thing as
On Sunday, 22 January 2017 at 13:34:10 UTC, Suliman wrote:
str[] = ""[];
That means copy the contents of the right hand array into the
location of the left hand array.
It copies data, that operation will never change pointers.
str = "";
would change the pointer.
You do not append to anything, only overwrite it. There is no
reallocation because
"aaa".length == "bbb".length.
I changed my code to:
str_ptr.length +=1;
str[] = ""[];
But now it's print length 4 before and after writing "bbb" to
`str`. I expected that size will be 3+4=7.
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 =
writeln("before: ", str_ptr.ptr);// address of structure
writeln(*str_ptr.ptr); // address of data
12 matches
Mail list logo