1

我在我的程序中使用了 unorder_map。问题是有时找不到我在地图中插入的密钥,我不知道原因,以下是我的代码:

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;

用法:

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);
            } 

程序插入了一个密钥对(192.168.2.20, 12),有时可以找到这个密钥,但偶尔找不到这个密钥,日志显示:

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

好奇怪,要搜索的key是<192.168.2.20, 12>,而且一直在map里,为什么find函数找不到,为什么在find( ) 称呼。find()前后,key是192.168.2.20,find的时候是184.28.16.107,len是12不对,为什么,key<184.28.16.107,12>是从哪里来的,任何人都可以找到我对 unordered_map 的使用是错误的?或者程序的逻辑错误,我为此花费了很多时间,但我找不到任何原因。你能帮助我吗?

4

1 回答 1

0

看起来很像 WhozCraig 分析的悬空指针问题。您可能会使用相同的ipString两次或三次...

您可以将您的配对中的“const char *”更改为字符串吗?我敢打赌它会解决你的问题。

于 2013-11-12T20:40:37.503 回答