digitalmars.D.learn - emplace, immutable members and undefined behaviour
- aewils (21/21) Nov 15 2015 According to http://dlang.org/const3.html any modification of
- Tobias Pankrath (11/16) Nov 15 2015 As long as there are no other references to the immutable members
- Tobias Pankrath (4/7) Nov 15 2015 You could also use the emplace version that takes untyped memory:
- aewils (4/14) Nov 15 2015 Missed that one...
According to http://dlang.org/const3.html any modification of immutable data causes undefined behaviour. Now I want to initialise a struct with immutable members in some malloc'd memory and found the emplace function. I came up with the following code: import core.stdc.stdlib; import std.conv; struct Point { immutable double x; immutable double y; } void main() { void* a = malloc(Point.sizeof); Point* p = cast(Point*) a; emplace!Point(p, 1.0, 2.0); } this compiles and runs fine. Because emplace expects a typed pointer, it actually modifies (*p).x and (*p).y As far as I understand, this causes undefined behavior. Are there any (safe) alternatives to this code other than making the immutable members mutable?
Nov 15 2015
this compiles and runs fine. Because emplace expects a typed pointer, it actually modifies (*p).x and (*p).y As far as I understand, this causes undefined behavior. Are there any (safe) alternatives to this code other than making the immutable members mutable?As long as there are no other references to the immutable members and you can guarantee that they are indeed in mutable memory (both guaranteed by malloc) you are safe. If you really don't want to write to any immutable member, you could do this: struct Point { double a; double b; } Point* p = (allocate memory from somewhere); emplace!Point(p, 1, 2); immutable(Point)* immutableP = cast(immutable(Point)*) p;
Nov 15 2015
On Sunday, 15 November 2015 at 10:59:43 UTC, Tobias Pankrath wrote:Point* p = (allocate memory from somewhere); emplace!Point(p, 1, 2); immutable(Point)* immutableP = cast(immutable(Point)*) p;You could also use the emplace version that takes untyped memory:
Nov 15 2015
On Sunday, 15 November 2015 at 11:12:02 UTC, Tobias Pankrath wrote:On Sunday, 15 November 2015 at 10:59:43 UTC, Tobias Pankrath wrote:Missed that one... Thanks!Point* p = (allocate memory from somewhere); emplace!Point(p, 1, 2); immutable(Point)* immutableP = cast(immutable(Point)*) p;You could also use the emplace version that takes untyped one.
Nov 15 2015