简体   繁体   English

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

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

I just wondered whether it is at all possible to use memory mapped files in portable C or C++. 我只是想知道是否可以在便携式C或C ++中使用内存映射文件。 I think not because as far as I know no standard recognizes the existence of memory mapped files. 我想不是因为据我所知,没有标准能够识别内存映射文件的存在。

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* ). 此外,我认为甚至不使用一块内存而不在那里构建一个对象(除了通过char* )。 So if we want to treat an existing mapped file as an array of integers that should be undefined behavior. 因此,如果我们想将现有的映射文件视为应该是未定义行为的整数数组。

So what's the situation regarding memory mapped files and the standard? 那么关于内存映射文件和标准的情况如何呢?

They do not. 他们不。 Memory mapping is generally offered by operating systems: C and C++ can also run without one. 内存映射通常由操作系统提供:C和C ++也可以在没有操作系统的情况下运行。 Imposing such availability on the range of platforms the languages target would be very limiting. 在语言所针对的平台范围上强加这种可用性将是非常有限的。

Files may also not be supported at all in freestanding environments, let alone memory mapped ones. 在独立环境中也可能根本不支持文件,更不用说内存映射的文件了。

I just wondered whether it is at all possible to use memory mapped files in portable C or C++. 我只是想知道是否可以在便携式C或C ++中使用内存映射文件。

Yes it is (in c++), see the boost.interprocess library http://www.boost.org/doc/libs/1_60_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file 是的(在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. 我想不是因为据我所知,没有标准能够识别内存映射文件的存在。

no it's not in a standard, it's in the boost library, which is almost as portable. 不,它不在标准中,它在升级库中,几乎同样便携。

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*). 此外,我认为甚至不使用一块内存而不在那里构建一个对象(除了通过char *)。 So if we want to treat an existing mapped file as an array of integers that should be undefined behavior. 因此,如果我们想将现有的映射文件视为应该是未定义行为的整数数组。

See the documentation above. 请参阅上面的文档。 You'll find that c++ objects map right into the shared memory, but are addressed through special 'offset' pointers from the beginning of the mapped memory region. 您会发现c ++对象直接映射到共享内存中,但是通过映射内存区域开头的特殊“偏移”指针进行寻址。

So what's the situation regarding memory mapped files and the standard? 那么关于内存映射文件和标准的情况如何呢?

There is no situation. 没有任何情况。 They're not part of the standard memory model. 它们不是标准内存模型的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM