Why would an empty std::map seg fault on the first insert?
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?)?
15
answers
|
Answer it!
|
If not, I'd suspect a wild pointer problem somewhere, overwriting the map...
(Sanity check: You aren't doing something odd like using a placement constructor, right?)
Personally, I think C++ is a horrible language, but that's just me.
Steve
By "properly," I mean that a data object declared as a static member of a class should have job-execution scope and should not be deleted and recreated in the midst of a job. The identical code compiled under GCC 4.0.1 on my Mac behaved that way, but with GCC 4.1.2 on a Linux server did not.
Steve
How do you instantiate the map in the code ?
Steve
clear() the map in the class ctor, the insert works fine.http://en.cppreference.com/w/cpp/container/map/map
![]() |
































