C string to D without memory allocation?

Jakob Ovrum via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 20 22:04:55 PST 2015


On Monday, 21 December 2015 at 06:00:45 UTC, Shriramana Sharma 
wrote:
> 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?

It could also simply have done the same as format("%p", p). The 
problem isn't that to!string is too accepting, but that it makes 
an unsafe assumption with an inconspicuous, generic interface.

> 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)?

Indeed.



More information about the Digitalmars-d-learn mailing list