Non-immutable char[] doesn't change?

Maxim Fomin maxim at maxim-fomin.ru
Sat Aug 18 08:05:56 PDT 2012


On Saturday, 18 August 2012 at 11:35:59 UTC, Salih Dincer wrote:
> So, that's not this a bug?
>
>   char[] a = [ 'a', 'b', 'c' ];
>   char[] b = cast(char[])"abc";
>
>   assert(a == b);       /* no problem! */
>   assert(typeid(a) ==
>          typeid(b));    /* no problem! */
>
> // v--- TOGGLE CODE
>   //a[0] = 'A';         /*
>   b[0] = 'A';           /* there is problem:
>                          * Segmentation fault
>                          */
>
> Thanx...

"abc" in that position is a string literal. String literals may 
be placed in write-protected memory (mentioned in web page about 
dmd on linux).

Anyway, type of "abc" is immutable(char)[] and casting just 
throws immutable away and doing this (even more writing to them) 
is a bad idea.


More information about the Digitalmars-d-learn mailing list