String Theory Questions
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Sep 13 10:31:07 PDT 2014
On Sat, 13 Sep 2014 17:09:56 +0000
WhatMeWorry via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:
> I guess I was expecting them to be equivalent. I can understand
> why both lengths are zero. But what is emptyStr.ptr doing with
> the 42F080 value? I presume this is a address? If so, what does
> this address contain and what is it used for?
it's used to keep "empty string". ;-)
note that "null string" and "empty string" aren't same things. arrays
are reference types and compiler magically knows that "null-arrays" are
just empty arrays (and you can assign 'null' to array to clear it).
but strings are special in one funny way: when compiler sees string
literal (i.e. quoted string) in source code, it actually generates
C-like zero-terminated string. this is to ease C interop, so we can
call C functions like this: `printf("my string!\n");` instead of this:
`printf("my string!\n".toStringz);`.
so your "empty string" is actually points to zero byte (and has zero
length, 'cause D strings aren't zero-terminated). and "null string" is
really "null", i.e. contains no data.
as for "immutable": it is done this way so compiler can place string
literals in read-only section of resulting binary. without immutability
calling `void foo (string s);` as `foo("wow!")` will require copying
string to heap first ('cause `s` contents allowed to be changed in
`foo()`).
adding implicit "copy-on-writing" semantic will increase compiler
complexity and hidden dynamic array struct size for virtually nothing.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20140913/aa8308f6/attachment.sig>
More information about the Digitalmars-d-learn
mailing list