January 23, 2017 Re: Can't understand if deallocation happens? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 nothing
>> writeln("old data: ", *(str.ptr-2)); // print c
>>
>> It's look like that there is some gap between data, because `d` minus 1 should be `c`, but `c` I am getting only if I am doing `-2`. Why?
>
> writeln("old data: ", cast(int)*(str.ptr-1));
>
> #old data: 0
>
> String is gaping by zero?? I thought they are continuously like
>
> abcdef
> ---↑
>
> Where `↑` is equal to `ptr`.
You have *two* distinct strings here. Why do you expect them to be sequential in memory? If you want them to be treated as one string, concatenate them.
auto s1 = "abc";
auto s2 = "def";
auto s3 = s1 ~ s2;
|
January 23, 2017 Re: Can't understand if deallocation happens? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | > 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?
|
January 23, 2017 Re: Can't understand if deallocation happens? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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"; writefln("%d",cast(ubyte)s.ptr[3]); // use '.ptr' to skip bounds check prints 0u |
Copyright © 1999-2021 by the D Language Foundation