Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 15, 2015 emplace, immutable members and undefined behaviour | ||||
---|---|---|---|---|
| ||||
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? |
November 15, 2015 Re: emplace, immutable members and undefined behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to aewils | >
> 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;
|
November 15, 2015 Re: emplace, immutable members and undefined behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | 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: http://dlang.org/phobos/std_conv.html#.emplace The last one. |
November 15, 2015 Re: emplace, immutable members and undefined behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | 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:
>>
>> 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: http://dlang.org/phobos/std_conv.html#.emplace The last one.
Missed that one...
Thanks!
|
Copyright © 1999-2021 by the D Language Foundation