Biggest problems w/ D - strings

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Mon Aug 13 06:20:06 PDT 2007


Vladimir Panteleev wrote:
> On Sat, 11 Aug 2007 00:49:01 +0300, C. Dunn <cdunn2001 at gmail.com> wrote:
> 
>> I have a field of n chars stored on disk.  It holds a null-terminated string, padded with zeroes.  It is amazingly difficult to compare such a char[n] with some other char[] (which, by the dictates of D, may or may not be null-terminated).
> 
> This reminds me of the Delphi string problem. Before Delphi 3 (OSLT), you had to do crazy stuff to get a PChar (char*) out of a string. Delphi "long" strings are somewhat similar to D's strings - they have a length property, and allow the string to contain zeroes. Because of that, you couldn't just typecast a string to a PChar, due to lack of a terminating zero. Borland solved the problem by having strings always have a null terminating byte at their end, thus allowing you to typecast a string directly to a PChar.
> 
> I noticed that memory for arrays are always allocated with an extra byte (internal/gc/gc.d, function _d_arraysetlengthT). I wonder if this is related and can be used for this purpose..?

It's probably to make sure a one-past-the-end pointer is also counted as 
a reference to a memory block by the garbage collector.
Though if it's initialized to 0, it could be used for the purpose you 
describe as a side-effect.



More information about the Digitalmars-d mailing list