C ++内存映射文件实现 [英] C++ Memory Mapped File Implementation

查看:137
本文介绍了C ++内存映射文件实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有问题实现内存映射文件。问题是。假设这返回有效的内存视图。

I have no problems implementing the memory mapped file. The question is. Assuming this returns a valid memory view.

void* pBuf = MapViewOfFile(hMapFile,  
        FILE_MAP_WRITE,
        0,                   
        0,      
        0);

我有替代使用memcpy给它的数据吗?例如我可以告诉我的应用程序在其中存储数据吗?我真的想要的东西像char * buffer = new char [1073741824]其中新的将数据放在内存映射文件中。这似乎是合乎逻辑的。或者我将不得不写一个包装器,使用memcpy写入内存视图?这将是disapointing。

Do i have alternatives to using memcpy to give data to it? for instance can i tell my application to store data in it? I really want something like char* buffer = new char[1073741824] where the new places the data in the memory mapped file. this seems logical. or will i have to write a wrapper that writes to the memory view using memcpy? that would be disapointing.

推荐答案

您可以使用 placement new 运算符,或者您可以将地址转换为要使用的结构或类。放置new的优点是类的构造函数将被调用。如果你使用从那时指针,你不需要复制数据,但读写直接到它。

You can use the placement new operator or you can cast the address to a structure or class you want to use. The advantage of placement new is that the constructor of the class will be called. If you use from then on that pointer you don't need to copy the data but read and write directly to it.

这篇关于C ++内存映射文件实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆