A debug class has started

ag0aep6g anonymous at example.com
Mon Dec 13 11:26:25 UTC 2021


On 13.12.21 12:09, drug wrote:
> That's because `str` is initialized by a literal and you can not change 
> it by definition. When you call `toStringz` it duplicates that literal 
> (adding terminating zero at the end) and the duplicate is mutable. I 
> would recommend do not use `toStringz` and just make duplicate of the 
> literal - https://run.dlang.io/is/vaosW0

 From the link:

> string str = "abc;def;ab".dup; // allocates the string in the heap
> char* w = cast(char*)str;
> writeln(replaceChar(w, str.length, ';', 'X'));

That still has undefined behavior. You cannot mutate the characters in a 
`string`. It doesn't matter if it came from a literal or `.dup`. Use 
`char[]` instead of `string`.


More information about the Digitalmars-d-learn mailing list