Obtaining an address given a (run time) variable name
evilrat
evilrat666 at gmail.com
Tue Jan 21 09:52:21 UTC 2025
On Tuesday, 21 January 2025 at 09:34:29 UTC, DLearner wrote:
> On Tuesday, 21 January 2025 at 05:54:48 UTC, evilrat wrote:
>> On Monday, 20 January 2025 at 22:32:02 UTC, DLearner wrote:
>>> It was because my understanding was that in certain
>>> situations the string construct did not just produce a
>>> character array - it also produced the (to me horrible)
>>> concept of adding a x'00' immediately after the last
>>> character of the array.
>>
>> This is only for compile-time known strings (aka literals) for
>> easy C interop, if you make strings at runtime they won't have
>> null terminator as a granted.
>
> Suppose we have:
> ```
> string str1 = "A" ~ "B";
> ```
>
> Does that produce:
> str1 == x'41' x'42' x'00' or
> == x'41' x'00' x'42' x'00' or
> == x'41' x'00' x'42' x'00' x'00'?
>
> And is str1.length == 2 or
> == 3 or
> == 4?
In this example it definitely should be length == 2, if it is not
then it is a bug.
It should only add null terminator AFTER the end of complete
string, but it is technically not a part of that string.
You know, accessing slice memory beyond its length is unsafe, you
should really never do this unless you are 100% know what exactly
are you doing.
Look, here is why it does that - in C it is basically a
convention that all strings is null terminated because this is
how standard library works and so everyone else has to follow.
D gives you a favor by doing this automatically without the need
of putting `string greeting = "hello\0"` or `"hello " ~ "world" ~
'\0'` everywhere in your code.
I hope this is now simple enough to understand.
More information about the Digitalmars-d-learn
mailing list