cstrncmp

Strcmp generate a core dump


So i have a std::unordered_map, i want to acces to strings stored intro this map. I want to search intro intro all words inside the map and compare with a given word. If the strings are same then continue execution of the if statement.

{
public:    
    bool CheckFoo(const char* word);

protected:
    typedef std::unordered_map<std::string, bool> word_map;
    word_map words_map;
};

bool CheckFoo(const char* word)
{
    if (words_map.empty())
    {
        return false;
    }

    auto it = words_map.begin();

    while (it != words_map.end())
    {
        const std::string &r = it->first;
        const char* tmp = word;

        if (strcmp(tmp, r.c_str() ) == 0)
        {
            return true;
        }
    }

    return false;
}

if (    CheckFoo("wordFoo") )
{
    //  bla bla
}

The problem is that those codes generate a .core dump file.. Do you see any mistakes in my codes?

The crash core analyze point me to strcmp line


Solution

  • Can't write comments yet but,

    Like Nunchy wrote, tmp is not defined in that context. I also noticed that your code never increments the map iterator, which would result in a never ending loop.

    I'm assuming you did not copy your actual code into your post but instead rewrote it hastily which resulted in some typos, but if not, try making sure you're using temp and not tmp in your call to strcmp, and make sure the loop actually increments the iterator.

    Like one of the comments on your post points out as well, make sure you actually have data in the map, and the function parameter.