String convention

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sat Jul 1 14:11:38 PDT 2006


Kirk McDonald wrote:
> Niklas Ulvinge wrote:
>> I just looked at D and it sounds rather interesting.

Always good to hear.

>> Now to my Q:
>> Strings in D starts with some data that is defining the length of the 
>> string.

Actually, the /reference/ to the (dynamic) string begins with that data 
(i.e. what in C would be the pointer to it is in D twice as long, the 
first half containing the length).
With static strings, the length is encoded in the type (i.e. a char[3] 
has length 3) and doesn't need to be stored separately.

>> Why did they decide to use this aproach?

Having constant-time access to the length of a string makes a lot of 
operations more efficient. Having it be separate from the string data 
enables you to do cool things like efficient string slicing. (see 
http://www.digitalmars.com/d/arrays.html#slicing )

Oh, and everything I'm saying about strings goes for *any* array type.

>> What is this 'data' at the beginning of the string?

An integer value: the length of the string.

> This is an implementation detail, and shouldn't matter to your code.
> 
>> This has a limitation, strings can't be longer than 'data' allows.

'data' is a size_t. The 'limit' you refer to is therefore /at least/ 
(the maximum size of addressable memory) - 1 (more on machines with 
weird pointer types or for array of elements with size > 1).

>> Is there a way around this?

Buy a computer that can address more memory at the same time (like a 
64-bit one, if you're coming from a 32-bit machine) and making sure your 
compiler generates appropriate code (i.e. use a 64-bit-aware compiler 
for a 64-bit platform).

>> Couldn't this be used with strings?
>> Then this would work:
>> string s = "IDK\0";
> 
> The D syntax is:
> 
> char[] s = "IDK";
> 
> The \0 is not needed as strings in D are not null-terminated. The length 
> of the string may be retrieved with "s.length".

Well, he wants to use it with printf("%s", ...), so then adding a null 
terminator at the end would probably be a good idea:

>> printf("%s",s);

Though, of course, writef is a better alternative.



More information about the Digitalmars-d mailing list