Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating point exception when storing something into unordered_map

I'm using unordered_map as a hashmap in C++, but whenever I try to store anything in there, I get:

Floating point exception: 8

Can anyone point out what the error is? The following is how I initialized my map (table_entry is just a struct):

std::tr1::unordered_map<unsigned short, table_entry*> forwarding_table;

Then I was putting an entry in by doing:

unsigned short dest_id = 0;    
table_entry *entry = (table_entry *)malloc(sizeof(table_entry));   
forwarding_table[dest_id] = entry;

My struct's definition is:

typedef struct table_entry {
    unsigned short next_hop;
    unsigned int cost;
} table_entry;

In terms of my compiler version, when I run g++ -v I get this:

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

like image 839
Penguinator Avatar asked Oct 21 '22 22:10

Penguinator


1 Answers

I recently encountered the same problem using a variety of instantiations of std::unordered_map<>. However, I can only reproduce the problem when the map is global to a shared object. If the map is declared as a global in the program, or as a local within a function, then the problem does not manifest.

(Note: I'm using GCC 4.9.4, 32-bit mode, with -std=c++11)

It appears that allocating the std::unordered_map<> on the heap solves my problem. Maybe it would solve yours? Consider replacing:

std::tr1::unordered_map<unsigned short, table_entry*> forwarding_table;

with

std::tr1::unordered_map<unsigned short, table_entry*>* forwarding_table;

And then updating uses of forwarding_table appropriately.

like image 173
Mike Spear Avatar answered Oct 23 '22 20:10

Mike Spear