Re: Consequences of casting away immutable from pointers

2018-01-05 Thread Patrick Schluter via Digitalmars-d-learn
On Friday, 5 January 2018 at 18:13:11 UTC, H. S. Teoh wrote: On Fri, Jan 05, 2018 at 05:50:34PM +, jmh530 via Digitalmars-d-learn wrote: Be careful with that: class C { int x; } immutable C c = new C(5); auto i = c.x; C y = cast(C) c; y.x = 10;

Re: Consequences of casting away immutable from pointers

2018-01-05 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 05, 2018 at 05:50:34PM +, jmh530 via Digitalmars-d-learn wrote: > On Friday, 5 January 2018 at 04:10:54 UTC, Steven Schveighoffer wrote: > > > > Yes, this is undefined behavior. > > > > https://dlang.org/spec/const3.html#removing_with_cast > > > > The compiler assumes x is going

Re: Consequences of casting away immutable from pointers

2018-01-05 Thread jmh530 via Digitalmars-d-learn
On Friday, 5 January 2018 at 04:10:54 UTC, Steven Schveighoffer wrote: Yes, this is undefined behavior. https://dlang.org/spec/const3.html#removing_with_cast The compiler assumes x is going to be 5 forever, so instead of loading the value at that address, it just loads 5 into a register (or

Re: Consequences of casting away immutable from pointers

2018-01-05 Thread jmh530 via Digitalmars-d-learn
On Friday, 5 January 2018 at 04:10:54 UTC, Steven Schveighoffer wrote: Yes, this is undefined behavior. https://dlang.org/spec/const3.html#removing_with_cast The compiler assumes x is going to be 5 forever, so instead of loading the value at that address, it just loads 5 into a register (or

Re: Consequences of casting away immutable from pointers

2018-01-04 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, January 05, 2018 04:16:48 Adam D. Ruppe via Digitalmars-d-learn wrote: > On Friday, 5 January 2018 at 04:10:54 UTC, Steven Schveighoffer > > wrote: > > The compiler assumes x is going to be 5 forever, so instead of > > loading the value at that address, it just loads 5 into a > > regist

Re: Consequences of casting away immutable from pointers

2018-01-04 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, January 04, 2018 23:10:54 Steven Schveighoffer via Digitalmars- d-learn wrote: > On 1/4/18 10:58 PM, jmh530 wrote: > > I'm trying to understand the consequences of casting away immutable from > > a pointer. The code below has some weird things going on like the > > pointers still point

Re: Consequences of casting away immutable from pointers

2018-01-04 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 5 January 2018 at 04:10:54 UTC, Steven Schveighoffer wrote: The compiler assumes x is going to be 5 forever, so instead of loading the value at that address, it just loads 5 into a register (or maybe it just folds x == 5 into true). I was curious what dmd did, and the disassembly in

Re: Consequences of casting away immutable from pointers

2018-01-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/4/18 10:58 PM, jmh530 wrote: I'm trying to understand the consequences of casting away immutable from a pointer. The code below has some weird things going on like the pointers still point to the correct address but when you dereference them they don't point to the correct value anymore.

Consequences of casting away immutable from pointers

2018-01-04 Thread jmh530 via Digitalmars-d-learn
I'm trying to understand the consequences of casting away immutable from a pointer. The code below has some weird things going on like the pointers still point to the correct address but when you dereference them they don't point to the correct value anymore. Should I just assume this is unde