将结构数组的指针添加到std :: map

时间:2018-06-12 20:08:00

标签: c++ pointers struct stdmap

我想用字符串保存数据索引数组,我认为我最好使用b来实现此目的。我有以下示例代码:

std::map

编译得很好,但是当我运行它时,我得到一个这样的mem转储:

 typedef struct MyType_s {
     long long timestamp;
     int cnt;
     bool parked;
 }MyType;

 static MyType *list = {0};
 static int listcnt  = 0;

 //-------------------------------------------------------------------------------------------------

 int map_add_item(std::map<std::string, MyType*> *pmap, std::string str, long long tmestmp)
 {
     if (listcnt == 0){
         list =(MyType*)malloc(sizeof(MyType));
         if (list)
             listcnt++;
         else
             return ENOMEM;
     }
     if (realloc(list,sizeof(MyType)*(++listcnt))==0)
         return ENOMEM;
     list->timestamp = tmestmp;
     if (!(str.length()&&tmestmp&&pmap))
         return EINVAL*-1;
     if (pmap->insert(std::make_pair(str, &list[listcnt-1])).second == false){
         pmap->find(str)->second->timestamp = tmestmp;
         return EEXIST*-1;
     }

     return OK;
 }

*** Error in `./std_map': double free or corruption (fasttop): 0x000000000226ec20 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f19fb6267e5] /lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f19fb62f37a] /lib/x86_64-linux-gnu/libc.so.6(+0x83350)[0x7f19fb632350] /lib/x86_64-linux-gnu/libc.so.6(realloc+0x179)[0x7f19fb633839] ./std_map[0x4013c8] ./std_map[0x40198d] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f19fb5cf830] ./std_map[0x401259] ======= Memory map: ======== 00400000-00404000 r-xp 00000000 08:02 6054199 /path/to/src/tmp/std_map 00604000-00605000 r--p 00004000 08:02 6054199 /path/to/src/tmp/std_map 00605000-00606000 rw-p 00005000 08:02 6054199 /path/to/src/tmp/std_map 0225d000-0228f000 rw-p 00000000 00:00 0 [heap] 7f19f4000000-7f19f4021000 rw-p 00000000 00:00 0 7f19f4021000-7f19f8000000 ---p 00000000 00:00 0 7f19fb2a6000-7f19fb3ae000 r-xp 00000000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so 7f19fb3ae000-7f19fb5ad000 ---p 00108000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so 7f19fb5ad000-7f19fb5ae000 r--p 00107000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so 7f19fb5ae000-7f19fb5af000 rw-p 00108000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so 7f19fb5af000-7f19fb76f000 r-xp 00000000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so 7f19fb76f000-7f19fb96f000 ---p 001c0000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so 7f19fb96f000-7f19fb973000 r--p 001c0000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so 7f19fb973000-7f19fb975000 rw-p 001c4000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so 7f19fb975000-7f19fb979000 rw-p 00000000 00:00 0 7f19fb979000-7f19fb98f000 r-xp 00000000 08:02 28971397 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f19fb98f000-7f19fbb8e000 ---p 00016000 08:02 28971397 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f19fbb8e000-7f19fbb8f000 rw-p 00015000 08:02 28971397 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f19fbb8f000-7f19fbd01000 r-xp 00000000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7f19fbd01000-7f19fbf01000 ---p 00172000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7f19fbf01000-7f19fbf0b000 r--p 00172000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7f19fbf0b000-7f19fbf0d000 rw-p 0017c000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7f19fbf0d000-7f19fbf11000 rw-p 00000000 00:00 0 7f19fbf11000-7f19fbf37000 r-xp 00000000 08:02 28971842 /lib/x86_64-linux-gnu/ld-2.23.so 7f19fc0fc000-7f19fc102000 rw-p 00000000 00:00 0 7f19fc135000-7f19fc136000 rw-p 00000000 00:00 0 7f19fc136000-7f19fc137000 r--p 00025000 08:02 28971842 /lib/x86_64-linux-gnu/ld-2.23.so 7f19fc137000-7f19fc138000 rw-p 00026000 08:02 28971842 /lib/x86_64-linux-gnu/ld-2.23.so 7f19fc138000-7f19fc139000 rw-p 00000000 00:00 0 7ffd68bf4000-7ffd68c16000 rw-p 00000000 00:00 0 [stack] 7ffd68d88000-7ffd68d8b000 r--p 00000000 00:00 0 [vvar] 7ffd68d8b000-7ffd68d8d000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] Command terminated 也没有给我更多信息。 这是在我从gdb

调用这样的函数之后
main()

我想知道我做错了什么,双重免费或腐败显然是一个提示,但我不知道如何解释这个问题我无法确定问题...

1 个答案:

答案 0 :(得分:0)

对于一个有效的解决方案,我用全局静态变量和地图中的指针替换了直接位于地图中的数据。另外,我从结构中删除了typedef,并将其声明为C ++方式,如:

std::map<std::string, MyType> *pmap

struct MyType {
     long long timestamp;
     int cnt;
     bool parked;
 };