Unexpected behavior when casting away immutable

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 22 21:06:20 PDT 2015


On Wednesday, 23 September 2015 at 03:50:44 UTC, Vladimir 
Panteleev wrote:
> 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.

I see. Thanks.


More information about the Digitalmars-d-learn mailing list