我想共享一個無序的映射(哈希映射),但它結束時浮動點異常在它嘗試在地圖中插入數據的行。編寫一個無序的映射到共享內存不工作
難道有人請幫忙瞭解我哪裏出錯了嗎?
#include <iostream>
#include <string>
#include <unordered_map>
#include <sys/ipc.h>
#include <sys/shm.h>
int main()
{
std::unordered_map<std::string,double> *tmp;
key_t key = 5678;
int shmid = shmget(key, 1000, IPC_CREAT | IPC_EXCL | 644);
if(shmid == -1){
std::cerr << "Failed to create the shared segment." << std::endl;
exit(-1);
}
void *addr = shmat(shmid, NULL, 0);
if(addr == (void*)-1){
std::cerr << "Failed to attach the segment to the process." << std::endl;
exit(-1);
}
tmp = static_cast< std::unordered_map<std::string,double>* >(addr);
tmp->insert (std::pair<std::string,double>("abc",1.2));
shmdt(addr);
return 0;
}
謝謝。
我不認爲shmat返回的類型std :: unordered_map *和void *與static_cast正常工作有關。 –
coelhudo
2012-08-01 14:48:58