C string to D without memory allocation?

Shriramana Sharma via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 20 22:00:45 PST 2015

Jakob Ovrum wrote:

> Use std.string.fromStringz. to!string assumes that pointers to
> characters are null-terminated strings which is not safe or
> general 

I suppose what you mean is, the onus of guaranteeing that const(char)* 
refers to a null-terminated string is upon the person calling the to! 
function? Yes I understand, and Phobos documentation does say that using a 
pointer for input makes this "@system". Wouldn't it be better to just reject 
pointer as input and force people to use fromStringz?

> > (unlike std.format, which safely assumes they are
> pointers to single characters); 

I see that "%s".format(str) where str is a const(char)* just prints the 
pointer value in hex. So perhaps one should say that std.format just treats 
it like any other pointer (and not specifically that it treats it as a 
pointer to a single char)?

> > it is a poor design. fromStringz
> is explicit about this assumption.

OK thank you.

Shriramana Sharma, Penguin #395953

More information about the Digitalmars-d-learn mailing list