简体   繁体   中英

unordered_map can't find the key

i used unorder_map in my program. the problem is it can't find the key which i have inserted in the map sometimes, i don't know the reason, the following is my code:

class FlowKey: public std::pair<const char*, unsigned int>
{
public:
    FlowKey(const char* s, unsigned int l): std::pair<const char*, unsigned int>(s, l) {
            fprintf(stderr, "raw string %s len %u, after pair, first %s second %u", s, l, this-     >first, this->second);
    }
    bool operator==(const FlowKey& c) const
    {
            if (this->second == c.second) {
                    if (0 == strncmp((char*)this->first, (char*)c.first, this->second)) {
                            fprintf(stderr, "key compare true\n");
                            return true;
                    } else {
                            fprintf(stderr, "this first ip:%s c first ip:%s\n", (char*)this->first, (char*)c.first);
                    }
            } else {
                    fprintf(stderr, "this second %d, c second %d\n", this->second, c.second);
            }
            fprintf(stderr, "key compare false\n");
            return false;
    }
};

class FlowKeyHash
{
 public:
    size_t operator()(const FlowKey& that ) const
    {
            unsigned int h = fnv_32a_buf(that.first, that.second, FNV_32A_INIT);
            fprintf(stderr, "ip %s len %u digest %u\n", that.first, that.second, h);
            return (size_t) h;
    }
};
typedef std::unordered_map<unsigned int, TcpInfo*, EmptyHash> UserAgent_Map;
typedef std::unordered_map<FlowKey, UserAgent_Map, FlowKeyHash> IP_Map;

usage:

FlowKey k(ipString, strlen(ipString));
IP_Map::iterator it = m_iptable.find(k);
if (it == m_iptable.end()) {
            //insert new entry to ip table and useragent table
            struct timeval time;
            gettimeofday(&time, NULL);
            fprintf(stderr, "ip not equel, insert: ip:(%s)ip-digest(%u), k first:%s k second %d tablesize %d hashsize %lu\n", ipString, ip_digest, k.first, k.second, m_tableSize, m_iptable.size());
            for(it = m_iptable.begin(); it != m_iptable.end(); ++it) {
                    fprintf(stderr, "key %s len %d\n", it->first.first, it->first.second);
            }
            ++m_tableSize;
    } else {
            //update ip table
            struct timeval time;
            gettimeofday(&time, NULL);
            UserAgent_Map::iterator it2 = it->second.find(user_agent_digest);
            if (it2 == it->second.end()) {
                    //insert new entry to user_agent table
                    ++m_tableSize;
                    fprintf(stderr, "user-agent not equel, insert: use-agent-digest(%u)\n", user_agent_digest);
            } 

the program has inserted a key pair(192.168.2.20, 12), it can be found this key sometime, but occasionally can't find this key, the log display:

raw key 192.168.2.20 len 12, after pair, first 192.168.2.20 second 12
ip 192.168.2.20 len 12 digest 1737338608
this first ip:192.168.2.20 c first ip:184.28.16.107
key compare false
ip 192.168.2.20 len 12 digest 1737338608
this first ip:192.168.2.20 c first ip:184.28.16.107
key compare false
ip not equel, insert: ip:(192.168.2.20)ip-digest(1737338608), k first:192.168.2.20 k second 12 tablesize 1 hashsize 2
key 192.168.2.20 len 12
key 184.28.16.107 len 12

it's so strange, the key which to be searched is <192.168.2.20, 12>, and it has been in the map, why the find function can't find it, why the searched key changed to 184.28.16.107 during the find() call. before and after find(), the key is 192.168.2.20, during the find, it's 184.28.16.107, and the len is 12 not right, why, and where is the key<184.28.16.107,12> from, anybody can find my usage of unordered_map is wrong? or the program's logical error, i cost a lot of time for this, but i can't find any reason. can you help me?

Seems a lot like a dangling pointer problem like WhozCraig analysed. You're may be using the same ipString twice or thrice...

Can you change the "const char*" in your pair for a string ? I bet it'll solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM