Segfault when writing to module-scope dynamic array

rombankzero bogomancer at gmail.com
Mon Nov 18 12:31:10 UTC 2019


Hi! Can anybody explain why this snippet segfaults when I try to 
run it:

> char[] array = new char[5];
>
> void main()
> {
>     array[0] = 'a';
> }

It works fine if I move the array into main, or (strangely) if I 
change its type to ubyte[] instead of char[], or if I merely read 
the array without modifying it. I assumed it would heap-allocate 
a mutable dynamic array before main runs, but maybe I've 
misunderstood what `new` expressions do at module scope? Or is 
this some special case for char arrays (strings)?

I really don't know what to make of it.


More information about the Digitalmars-d-learn mailing list