簡體   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