Segfault upon modifying immutable AA in static this

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 24 10:11:02 PST 2015


On Saturday, 24 January 2015 at 13:24:02 UTC, Nordlöw wrote:
> This snippet
>
>     static immutable words = [ `zero`, `one`, `two` ];
>
>     static immutable ubyte[string] wordsAA;
>
>     static this()
>     {
>         foreach (ubyte i, e; words) { wordsAA[e] = i; }
>     }
>
> compiles and links on dmd git master but run segfaults.
>
> Removing immutable qualifier from wordsAA avoids the segfeault.
>
> Are static module ctors allowed to modify immutable data or 
> this a DMD bug?

They are definitely allowed to initialize immutable data, i.e. 
write once. I don't know if you're allowed to change it 
repeatedly.

You can change your code to write only once:

static this()
{
     import std.exception: assumeUnique;
     ubyte[string] tmp;
     foreach (ubyte i, e; words) { tmp[e] = i; }
     wordsAA = assumeUnique(tmp); /* Don't alter tmp from here on. 
*/
}


More information about the Digitalmars-d-learn mailing list