简体   繁体   中英

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 . 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.

Edit: another possibility is that you're indexing into nodes incorrectly. Check that nodes[*it + 1] is valid.

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