Unexpected behavior when casting away immutable

Vladimir Panteleev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 22 20:50:43 PDT 2015


On Wednesday, 23 September 2015 at 03:39:02 UTC, Mike Parker 
wrote:
> ```
> immutable int x = 10;
> int* px = cast(int*)&x;
> *px = 9;
> writeln(x);
> ```
>
> It prints 10, where I expected 9. This is on Windows. I'm 
> curious if anyone knows why it happens.

Essentially, because x is immutable, the compiler optimizes 
writeln(x) to writeln(10). This seems to happen even without -O.



More information about the Digitalmars-d-learn mailing list