Why patching a string using .ptr fails under Linux64 but not under Win32?

d, pointers, portability, string

Solution

one thing to remember is that `string` is an alias to `(immutable char)[]` which means that trying to write to the elements is undefined behavior

one of the reasons that I can think the behavior differs is that under linux64 the compiler puts the string data in write-protected memory, which means that `*cast (char*) (b.ptr + Index) = '#';` fails (either silently or with segfault)

Problem

Why the small sample below fails under Linux64 but not under Windows32? ``` module test; import std.string, std.stdio; void main(string[] args) { string a = "abcd=1234"; auto b = &a; auto Index = indexOf(*b, '='); if (Index != -1) *cast (char*) (b.ptr + Index) = '#'; writeln(*b); readln; } ```

Original source