Question

I'm trying to use a std::map to hold config details, however I'm getting different behaviour between Windows (Windows 8 64-bit) and Linux (Ubuntu 12.04 64-bit)

Below is the code snippet:

std::map<std::string, std::string> testMap;

testMap.insert(std::pair<std::string, std::string>("test1", "value1"));
testMap.insert(std::pair<std::string, std::string>("test2", "value2"));

std::cout << testMap["test1"] << std::endl;

On windows this returns "value1" as expected, however on Linux this cause a Seg Fault.

Can anyone point me in the right direction?

Was it helpful?

Solution

The above code is fine and it works correctly on Linux machine.You have mentioned that you are getting the segmentation fault, while accessing the line, which indicates that your other logic of your program is doing some sort of memory corruption.

std::cout << testMap["test1"] << std::endl;

Heap corruption is often detected after the real corruption has already occurred by some DLL/module loaded within your process and current logic(where program is crashing) could be victim. However as this problem is observed on Linux machine, you many want to use Valgrind.This is the way you should attach your program(a.out).

$ valgrind --tool=memcheck --db-attach=yes ./a.out

Valgrind would attach your program(automatically) in the debugger when your program detects memory error so that you can do live debugging(GDB). This way you would get the root cause of the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top