On Mon, 1 Mar 2021, Thomas Bleher via Gcc-help wrote:
I'm wondering if G++ has special guarantees for accessing mmapped memory. My use-case: - I have some large data sets (>10GB) in a file, mostly float arrays, plus some POD management structs - I need to share this data read-only between unrelated processes To support this, I'd like to mmap the file containing the data into the processes that access it (properly aligned of course), and read it directly. I cannot memcpy the whole content, since then the data sharing between processes would be lost. However, my understanding is that C++ basically only allows memcpy from such data, but no direct access e.g. as float array (which would be UB in C++, because of the object lifetime rules).
If you really wanted, you could write a wrapper so that operator[] does a memcpy of just the 4 bytes you want into a float and returns that float, which would be optimized to a plain read.
My question: does g++ guarantee anything beyond ISO C++ in this regard? Using mmap to share data between processes sounds very useful, so it would be a pity if this was impossible.
I don't know how official this is, but I believe you are safe. mmap is an opaque function, for all gcc knows, mmap may have actually created objects of the expected types in those locations, so gcc cannot make any optimization that would break that case.
Most issues come if you try to do 2 conflicting operations on the same memory location, which is not your case.
It might cause issues in some intrusive debugging mode that would track all objects, but I don't think I've seen a tool like that.
-- Marc Glisse