Reading POD data directly from mmapped memory in C++

Thomas Bleher ThomasBleher@gmx.de
Mon Mar 1 22:21:33 GMT 2021


Hello,

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).
See
https://stackoverflow.com/questions/55034863/dealing-with-undefined-behavior-when-using-reinterpret-cast-in-a-memory-mapping
for a very similar question from someone else.

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.

Alternatively, I could write the direct data access in C, and call that
from C++. Basically, have a C function that mmaps the file, finds the
start of the float array in the file, and returns a 'const float*'
pointing to the start of the array. My understanding is that this is
legal in C. Would the returned pointer also be legal to use in C++?
(Assuming gcc and g++ as compiler; I'm asking about the guarantees GCC
gives beyond ISO C and ISO C++).

Thanks,
Thomas


More information about the Gcc-help mailing list