Why would an empty std::map seg fault on the first insert? Answered
I've got a weird C++ problem which make no sense to me. I have a class containing a
std::map<> data member, which I fill during the initialization process. On my Mac (OSX 10.5.8, GCC 4.0.1), the map is filled perfectly fine, and the data is accessed when the job runs with no problem. When I build and run on Linux (RHEL 5.6, GCC 4.1.2), the
std::map<> itself seg-faults the first time something is inserted.
I've tried the insert with both
table[key] = value and with
table.insert(std::pair<>(key,value)) and I get the identical result. For some reason,
std::map<>::insert_unique() doesn't recognize that the map is empty, and it tries to decrement one position before begin(), triggering the seg-fault.
Am I doing something wrong? Do I need to explicitly zero the map (the constructor should do that itself, shouldn't it?)?