Why does the STL display the main landfill?

So, I have a situation where I need to see if an object is on my stl map. If it is not, I am going to add it.

char symbolName[] = { 'H', 'e', 'l', 'l', 'o', '\0' }; map<string,TheObject> theMap; if (theMap.find(symbolName)==theMap.end()) { TheObject theObject(symbolName); theMap.insert(pair<string, TheObject>(symbolName, theObject)); } 

I get a core dump: theMap.find when the object is not already on the map. Presumably, if the item is not on the map, it should return an iterator equal to display :: end

What's going on here?

GCC: 3.4.6

+4
source share
10 answers

It can crash due to many reasons. Not knowing the definition of constructors no less than TheObject , I think that we can largely guess the problem. Your code looks good so far, but it can be simplified:

 char symbolName[] = "Hello"; map<string,TheObject> theMap; theMap.insert(make_pair(symbolName, TheObject(symbolName))); 

It will do nothing if the character is already displayed, discarding the new TheObject.

+4
source

Why not just do it like this?

 char symbolName[] = "hello"; theMap.insert(pair<string, TheObject>(symbolName, TheObject(symbolName))); 

If your map is map<string, TheObject> , then you will get a core dump if you try to find NULL:

 // This will core dump: char *symbolName = NULL; // Oops! theMap.find(symbolName); // Kabang! 
+4
source

Make sure your STL map is empty () before doing find (). Some STL implementations are errors when find () is executed on an empty STL map.

+2
source

By the way, two simplifications are possible.

First use make_pair instead of using the pair constructor. This means that you do not need to explicitly specify type arguments:

 theMap.insert(make_pair(symbolName, theObject)); 

Secondly, instead of code, you can simply write:

 theMap[symbolName] = theObject; 
+2
source

Submit your real code or at least a complete example demonstrating the behavior you are describing. Sometimes the problem is the subtlety lost in translation when you try to debug make-ver code written only for posting on forums like this.

+2
source

This code should work fine.

The only possible problem is the implementation of TheObject, but it should not affect the find call.

In addition, there may be some kind of unrelated problem, for example, a buffer overflow due to some code being executed before it.

+1
source

In addition to simplifying the code:

Use std :: string instead of char [] and use it inside your class definition for TheObject.

Not sure what your TheObject class does with a char array, but if you use a map outside the scope of this function, for example. using it as a return value, I'm sure there is somewhere an object pointing to the symbolName address, which is no longer defined.

+1
source

I do not know why you get a kernel dump; The code works for me.

It is possible to make your code simpler and more understandable:

  • As someone else noted, you can simplify the input in theMap[symbolName] = TheObject(symbolName)

  • Maybe I'm wrong, but as far as I know, {'a', 'b', '\ 0'} matches "abc", so you can simplify to char symbolName[] = "Hello"; I have the impression that the non-idiomatic definition confuses some people here.

0
source

Check if you use it in the constructor. I had a similar problem when using the map constructor inside.

0
source

I fixed my problem like this using pointers to my map objects. Since they were used in the constructor, they had to be pointers, and then inside the constructor, which I am new to them.

0
source

All Articles