我unordered_map在 C++ 中用作哈希图,但是每当我尝试在其中存储任何内容时,我都会得到:
Floating point exception: 8
谁能指出错误是什么?以下是我如何初始化我的地图(table_entry只是一个结构):
std::tr1::unordered_map<unsigned short, table_entry*> forwarding_table;
然后我通过以下方式输入了一个条目:
unsigned short dest_id = 0;
table_entry *entry = (table_entry *)malloc(sizeof(table_entry));
forwarding_table[dest_id] = entry;
Run Code Online (Sandbox Code Playgroud)
我的结构定义是:
typedef struct table_entry {
unsigned short next_hop;
unsigned int cost;
} table_entry;
Run Code Online (Sandbox Code Playgroud)
就我的编译器版本而言,当我运行时,g++ -v我得到了这个:
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1
我最近使用各种实例化遇到了同样的问题std::unordered_map<>。但是,只有当映射对于共享对象是全局的时,我才能重现该问题。如果映射在程序中被声明为全局的,或者在函数中被声明为局部的,那么问题就不会显现出来。
(注意:我使用的是 GCC 4.9.4,32 位模式,带有 -std=c++11)
看来std::unordered_map<>在堆上分配 解决了我的问题。也许它能解决你的问题?考虑更换:
std::tr1::unordered_map<unsigned short, table_entry*> forwarding_table;
Run Code Online (Sandbox Code Playgroud)
和
std::tr1::unordered_map<unsigned short, table_entry*>* forwarding_table;
Run Code Online (Sandbox Code Playgroud)
然后forwarding_table适当更新用途。
| 归档时间: |
|
| 查看次数: |
2147 次 |
| 最近记录: |