Re: Segfault when writing to module-scope dynamic array

2019-11-18 Thread rombankzero via Digitalmars-d-learn
On Monday, 18 November 2019 at 13:28:26 UTC, Adam D. Ruppe wrote: But it is rare and if you aren't specifically looking for it knowing the details, it isn't right. And since the syntax looks so normal it trips up a LOT of people. I think the compiler should probably start to error on it, and

Re: Segfault when writing to module-scope dynamic array

2019-11-18 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 18 November 2019 at 13:22:20 UTC, rombankzero wrote: Yeah, that appears to be what's happening. For the record, I tried it on both Linux and Windows, and only got a segfault on Linux. Yeah, that's consistent with other strings too. (The D type system calls both immutable, but the

Re: Segfault when writing to module-scope dynamic array

2019-11-18 Thread rombankzero via Digitalmars-d-learn
On Monday, 18 November 2019 at 12:42:26 UTC, Adam D. Ruppe wrote: 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. Yeah, that appears to be what's happening. For

Re: Segfault when writing to module-scope dynamic array

2019-11-18 Thread Adam D. Ruppe via Digitalmars-d-learn
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

Segfault when writing to module-scope dynamic array

2019-11-18 Thread rombankzero via Digitalmars-d-learn
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