No, this is not safe. The address of the first byte in the memory mapped file cannot be guaranteed to be the same between invocations.
Boost's memory mapped file takes a hint
, but it is only a hint.
So in the first invocation, the memory might be located at 0xBAADF00D
. On the second invocation, it might be located at 0xF00DBAAD
. Pointers within the structure would no longer be valid, as they would point to memory around 0xBAADF00D
rather than around 0xF00DBAAD
.
As a serious problem, it might usually work in testing, as the hint
might usually be obeyed -- but sometimes there will already be stuff at that memory address, and the hint
will have to be ignored.
(There are other parts that make this really hard -- but the above makes it pretty infeasible).
Now, strategies like this can work, but require very intrusive introspection of the data structures in question. You'd have to do a pass like DLL readdressing in order to get the data structures to have the correct offsets, and by that point you might as well just serialize it flat.