toStringz or not toStringz

Walter Bright newshound2 at digitalmars.com
Fri Jul 8 10:59:47 PDT 2011


On 7/8/2011 4:53 AM, Regan Heath wrote:
> On Fri, 08 Jul 2011 10:49:08 +0100, Walter Bright <newshound2 at digitalmars.com>
> wrote:
>
>> On 7/8/2011 2:26 AM, Regan Heath wrote:
>>> Why can't we have the
>>> compiler call it automatically whenever we pass a string, or char[] to an extern
>>> "C" function, where the parameter is defined as char*?
>>
>> Because char* in C does not necessarily mean "zero terminated string".
>
> Sure, but in many (most?) cases it does. And in those cases where it doesn't you
> could argue ubyte* or byte* should have been used in the D extern "C"
> declaration instead. Plus, in those cases, worst case scenario, D passes an
> extra \0 byte to those functions which either ignore it because they were also
> passed a length, or expect a fixed sized structure, or .. I don't know what as I
> can't imagine another case where char* would be used without it being a "zero
> terminated string", or passing/knowing the length ahead of time.

In the worst case, you're adding an extra memory allocation and function call 
overhead (that is hidden to the user, and not turn-off-able). This is not 
acceptable when interfacing to C.


> D is already allocating an extra \0 byte for string constants right?

Yes, but in a way that is essentially free.


More information about the Digitalmars-d mailing list