本文介绍了如何使用mmap指向STL类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:35