简体   繁体   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]++;
    }
}  

This function is producing error 该函数产生错误

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

Can someone tell me why is it coming and what it means? 有人可以告诉我为什么会来,这意味着什么吗? Thanks in advance. 提前致谢。

Well, one issue I see is that you're not checking to see if vertex was actually found in edges . 好吧,我看到的一个问题是,您没有检查是否实际上在edges找到了vertex You are probably dereferencing memory you don't own. 您可能正在取消引用您不拥有的内存。

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]++;
        }
    }
}

Next time, try running your app through GDB, and check your stack trace. 下次,尝试通过GDB运行您的应用程序,并检查堆栈跟踪。

Edit: another possibility is that you're indexing into nodes incorrectly. 编辑:另一种可能性是您索引到nodes不正确。 Check that nodes[*it + 1] is valid. 检查nodes[*it + 1]是否有效。

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

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