On Monday, 18 November 2019 at 12:31:10 UTC, rombankzero wrote:
char[] array = new char[5];
It is almost always wrong to new things at module, class, or
struct scope, since this create it in the static data segment.
My guess is the compiler is seeing a static string and
incorrectly putting it in the read-only segment with the other
strings, and then writing to it thus triggers that segfault.
just a guess, and if correct, this is a dmd bug.
But I'd still recommend doing the `new` inside a function/module
constructor anyway. That takes the static aspect out entirely and
prevents some surprising bugs (right now the new is run at CTFE,
not a huge deal here but with like classes it surprises a lot of
people as the default pointer is shared between instances!)
I assumed it would heap-allocate a mutable dynamic array before
main runs
but yeah this is NOT what it does. It runs new at *compile time*
which means that `array` variable is pointing to static memory,
it is not heap allocated at all.