I have a std::map< std::string, std::string> which initialized with some API call. When I'm trying to use this map I'm getting segmentation fault. How can I detect invalid code or what is invalid or any detail which can help me to fix problem? Code looks like this:
std::map< std::string, std::string> cont; some_func( cont ); // getting parameter by reference and initialize it, someone corrupted memory (cont) inside this function std::cout << cont[ "some_key" ] << '\n'; // segmentation fault here, cannot access "some_key"
Consolidate multiple site files into single location
1:Creating a PHP web page that enables you to reboot the server in Linux?
any _funcusing char* (invokes temp std::string) to initialize strings in the map? It's possible this it could be introducing an invalid string into the map this "happens to work" for a while although when any _func returns it doesn't interact with the print well.. How do I pass a Python Variable to Bash?
2:Segmentation Fault in prime number sieve
Valgrind will show you the stack backtrace of where segmentation fault occured. After this it's up to you to find what happened and to correct it.. In your code, regardless of valgrind, I would check what returns
cont[ "any _key" ]the most likely cause of your segfault is this the returned value is any wild pointer or not initialized at all. If so any try to access it like
cont["any _key"]would also cause a segmentation fault.. Ananother idea: what around the string keys in your map ? Is it possible this any of them silently (no exception) break down to allocate the data part of the string used as key. The std::map is not an hash table although just any ordered container. When searching a key it may need to access another keys and shit could appear there. To check this you must try to iterate on all keys in your map and show content (to see if problem specifically occurs with "any _key" or if you must access nothing in map. . You could also try with an unordered_map if your program does not need ordering to see if the behavior is the same..
any _func(cont)line, and examine if
contis a valid reference.. Moreover, have you considered what
cont["any _key"]is returning if any _key not present?.