繁体   English   中英

C ++程序中的未知错误

[英]Unknown error in a C++ program

void change_degree(vector<int> &nodes, map<int, vector<int> > &edges, int vertex){
    map<int, vector<int> >::iterator ite;
    ite = edges.find(vertex);
    vector<int> temp = (*ite).second;
    vector<int>::iterator it;
    for(it = temp.begin(); it != temp.end(); it++){
        cout << *it;
        if(nodes[*it + 1] > 1)
            nodes[*it + 1]++;
    }
}  

该函数产生错误

*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x09c930e0 ***  

有人可以告诉我为什么会来,这意味着什么吗? 提前致谢。

好吧,我看到的一个问题是,您没有检查是否实际上在edges找到了vertex 您可能正在取消引用您不拥有的内存。

void change_degree(vector<int> &nodes, map<int, vector<int> > &edges, int vertex){
    map<int, vector<int> >::iterator ite = edges.find(vertex);
    if (ite != edges.end()) {  // <-- this is what you're missing
        vector<int> temp = (*ite).second;  // <-- this is probably where you're dying
        vector<int>::iterator it;
        for(it = temp.begin(); it != temp.end(); it++){
            cout << *it;
            if(nodes[*it + 1] > 1)  // <-- you could also be crashing here
                nodes[*it + 1]++;
        }
    }
}

下次,尝试通过GDB运行您的应用程序,并检查堆栈跟踪。

编辑:另一种可能性是您索引到nodes不正确。 检查nodes[*it + 1]是否有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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