简体   繁体   中英

Deleting pointers through a vector of pointers

So I'm working on a network flow problem and have to be able to delete the right half of my bipartite graph in order to deal with multiple graphs at once. Here's how I have my node and edge classes set up:

class Node {
public:
    Node();
    int id;
    int visited;
    Node_Type type;
    vector <bool> letters;
    vector <class Edge *> adj; // Adjacency list
    class Edge *backedge;
};

class Edge {
public:
    Node *to;
    Node *from;
    Edge *reverse; // Edge with opposite to/from
    int original; // 1 on normal
    int residual; // 0 on normal
};

And a potential graph can be seen in this image:

图形图像

Where my goal is to delete all edges and nodes to the right of the second column.

I have all my nodes organized in a vector of Node pointers, indexed from left to right/top to bottom, and I'm trying to traverse that vector and delete any edges contained in a node's adjacency list that are within the second and third columns or the third and fourth columns. After which I'll traverse backwards and delete the sink node as well as the third column's nodes, then finally resize the nodes vector to only accommodate the first two columns of nodes.

Here's how I'm doing that:

void DeleteHalfGraph() {
    int i, j;

    // Delete all edges between dice, words, and the sink
    for(i = 1; i < nodes.size(); i++) {
        if(nodes[i]->type == DICE) { // DICE refers to the second column of nodes
            for(j = 0; j < nodes[i]->adj.size(); j++) {
                if(nodes[i]->adj[j]->to->type == WORD) {
                // WORD refers to the third column of nodes
                    delete nodes[i]->adj[j];
                }
            }
        }
        else if(nodes[i]->type == WORD || nodes[i]->type == SINK) {
        // SINK refers to the 4th column of nodes
            for(j = 0; j < nodes[i]->adj.size(); j++) {
                delete nodes[i]->adj[j];
            }
        }
    }

    // Delete all nodes now not connected to edges
    // minNodes = 5, size of the vector without the 3rd/4th columns
    for(i = nodes.size() - 1; i >= minNodes; i--) {
        if(nodes[i]->backedge != NULL) delete nodes[i]->backedge;
        delete nodes[i];
    }

    nodes.resize(minNodes);
}

The error I'm getting when compiling is:

*** Error in `./worddice': malloc(): memory corruption (fast): 0x0000000000c69af0 ***

I very well just may not be understanding my pointers correctly as I haven't dealt with deallocating memory like this recently. Regardless, where am I going wrong? Any help is greatly appreciated.

The solution actually ended up being pretty simple: I wasn't resizing my adjacency list after deleting edges from it, so when I went to go add new edges to the vector for the next graph, the empty pointers would still be left sitting there (which I tried accessing in other parts of my program). Removing those from my adjacency vector fixed the problem.

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