问题描述
我有一个mmap类型的字符指针
char * ptr;
ptr = *)mmap(0,FILESIZE,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
这是我早期的代码。但现在我想使用映射而不是char *作为需求的变化。
现在,我的地图被声明为
map< int,string> i_s_map;
如何将我的mmap调用更改为指向地图?
您不想将STL容器存储在共享内存中,至少不会共享它们。原因是它们严重依赖于堆分配,所以现成的 std :: map
将保存来自不同进程的虚拟地址空间的指针。 p>
查看来处理C ++中的这种情况。
I have a mmap typecast to a char pointer
char *ptr;
ptr = (char *)mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
This was my earlier code. But now I want to use a map instead of char * as the requirements changed.
Now, my map is declared asmap < int, string > i_s_map;
How do I change my mmap call to point to the map?
You don't want to store STL containers in shared memory, at least not share them. The reason is that they rely heavily on heap allocation, so out-of-the-box std::map
will hold pointers from virtual address space of a different process.
Take a look at boost::interprocess
for a way to deal with this situation in C++.
这篇关于如何使用mmap指向STL类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!