why is string not implicit convertable to const(char*) ?

Wouter Verhelst wouter at grep.be
Thu Jul 5 18:59:26 PDT 2012


Jonathan M Davis <jmdavisProg at gmx.com> writes:

> On Thursday, July 05, 2012 18:57:05 Wouter Verhelst wrote:
>> To be fair, there are a _few_ areas in which zero-terminated strings may
>> possibly outperform zero-terminated strings (appending data in the case
>> where you know the memory block is large enough, for instance). But
>> they're far and few between, and it would indeed be silly to switch to
>> zero-terminated strings.
>
> Actually, I'd expect a string that maintains its length to beat a zero-
> terminated string at that - especially because you'd have to already know the 
> string's length to pull that off, which is O(n) for zero-terminated strings. 
> The _only_ time that the zero-terminated string might outperform the one which 
> maintained its length when you to append is if you already happen to know the 
> length of the string being appended to and the string being appended (which 
> you wouldn't normally with zero-terminated strings), because then the zero-
> terminated string would have one more byte to copy as part of its memcpy than 
> the other string would, but the other string would have to adjust its length, 
> making it cost _slightly_ more.

That's what I meant, yes.

> But really, given the overal costs of zero-
> terminated length, it would be ridiculous to even count that extra bit of 
> performance given the _huge_ performance losses everywhere else with them.

Absolutely.

> The _only_ valid excuse that I'm aware of for picking such a horrid design is 
> the fact that it costs extra memory to maintain the length of an array along 
> with the array, and when C was created, they cared a _lot_ more about memory 
> usage than we do today. So, regardless of what the pros or cons were in the 
> short run, in the long run, their decision was a very poor one that pretty 
> much no one has duplicated.

Well, really, strings in C are just a special case of arrays (as is true
in D as well), and arrays in C are just a special case of pointers
(which isn't true in D). That means the language is fairly compact,
which also means the compiler has much lower resource
requirements. I think that, much more than any requirements at runtime,
has driven the choice for zero-terminated strings.

Just for comparison, what happens to DMD's memory usage when you do
extensive templating wouldn't have been possible back in 1969 ;-)

-- 
The volume of a pizza of thickness a and radius z can be described by
the following formula:

pi zz a


More information about the Digitalmars-d-learn mailing list