是否有任何 C 或 C++ 标准识别内存映射文件的存在?

Does any C or C++ standard recognize the existence of memory mapped files?

我只是想知道是否可以在可移植的 C 或 C++ 中使用内存映射文件。我认为不是,因为据我所知没有标准承认内存映射文件的存在。

使用内存映射可以在两个地址处拥有相同的字节。另外,我认为如果不首先在那里构造一个对象,甚至不可能使用一块内存(通过 char* 除外)。因此,如果我们想将现有的映射文件视为一个整数数组,这应该是未定义的行为。

那么内存映射文件和标准是什么情况呢?

他们没有。内存映射通常由操作系统提供:C 和 C++ 也可以 运行 没有。在语言目标平台范围内施加这种可用性将是非常有限的。

在独立环境中也可能根本不支持文件,更不用说内存映射的了。

I just wondered whether it is at all possible to use memory mapped files in portable C or C++.

是的(在 c++ 中),参见 boost.interprocess 库 http://www.boost.org/doc/libs/1_60_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file

I think not because as far as I know no standard recognizes the existence of memory mapped files.

不,它不在标准中,它在 boost 库中,它几乎是可移植的。

Using memory mappings it is possible to have the same byte at two addresses. Also, I think it is not possible to even use a piece of memory without constructing an object there first (except through char*). So if we want to treat an existing mapped file as an array of integers that should be undefined behavior.

请参阅上面的文档。您会发现 C++ 对象直接映射到共享内存中,但通过特殊的 'offset' 指针从映射内存区域的开头寻址。

So what's the situation regarding memory mapped files and the standard?

没有任何情况。它们不是标准内存模型的一部分。