A debug class has started

drug drug2004 at bk.ru
Mon Dec 13 11:47:10 UTC 2021


On 13.12.2021 14:26, ag0aep6g wrote:
> 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`.

You're right. I forget to change `string str` to `auto str` or `char[] str`.


More information about the Digitalmars-d-learn mailing list