delete pointer in vector

Hello!

I'm tying to create a program that evaluates all possible actions for a certain problem.

So what i'm basically trying to do is to create a sequence of actions, evaluate them to check if it's the best sequence, change the sequence, evaluate again and so on until all possible scenarios are exhausted, leaving the best one in the end.

My approach to this at first was to create a tree of all possible options and then evaluate each branch. Since there are a lot of possible cases i ran out of memory while the program was still creating the tree. I changed this to create just a branch, evaluate it and then modify it.

First of all my question is: is this the best approach for this type of problem or can you point me to a better/faster way to solve the problem?

Second i was still getting memory problems. I declared a class tNode and declared a vector<tNode*> branch. Then i created all the nodes i needed for that branch with branch.push_back( new tNode() ). When i wanted to modify the branch i simply used branch.pop_back() and again a branch.push_back( new tNode() ). I figured i was getting the problem because although i clear the vector, i don't actually clear the reference in memory. Is this correct? If so, how can i actually delete the memory space and not just the pointer in the vector?

Please tell me if ths is not enough info.

Thank you very much

nt
I would suggest that you post some of the code giving rise to the issue.

However, for every new that uses memory on the heap there MUST be a coresponding delete. Unless you are going to assign a new block of memory to the pointer then assign NULL to the pointer imediately after the delete so that you don't attempt to delete the same pointer twice.
So this is my class tNode
1
2
3
4
5
6
7
8
9
10
11
12
13
class tNode {
public:
    tNode( int nodeNumber ) { dNodeNumber = nodeNumber; };
    ~tNode();

    void clearChildren() { possibleChildren.clear(); };
    void eraseChild( int index ) { possibleChildren.erase( possibleChildren.begin() + index ); };
    void addChild( int newChild ) { possibleChildren.push_back( newChild ); };
    void changeChildren( vector<int> possibilities ) { possibleChildren.clear(); possibleChildren = possibilities; };

    int dNodeNumber;
    vector<int> possibleChildren;
};


then along the code i declare a
vector<tNode*> branch;

I created a function to populate the vector
bool createBranch( vector<tNode*>& branch ) {//...}

I insert new elements in branch using the possibleChildren vector. In each iteration i have a series of conditions that populate possibleChildren of the last node in branch and then i create a new node with dNodeNumber equal to the last element of possibleChildren and do possibleChildren.pop_back().

this is what i do to insert a new element in branch:
1
2
3
branch.push_back( new tNode(branch[branch.size()-1]->possibleChildren[branch[branch.size()-1]->possibleChildren.size()-1]) );
// basically going to the last element of possibleChildren in the last node of branch
branch[branch.size()-2]->possibleChildren.pop_back();


i have a stopping condition to know that branch is complete (and the last element of branch has possibleChildren with size 0 ).

what i intend to do in the next iteration is:
iterate branch from end to begining and remove elements that have possibleChildren with size=0 until the first one that has possibleChildren with size!=0 (thus removing all actions that have no possible action after). then repopulate the branch.

this process would go until only the root node with possibleChildren's size=0 exists in the branch.

so:
populating the branch the first time works (i printed the content of branch and everything was as expected everytime);
this implies repopulating is also working because i use the same function and i tried to manually introduce some random nodes in a branch just to test this and it worked as expected;
what i think is not working is the deletion of elements in branch.

at first i was simply doing
1
2
3
4
5
6
7
        // Remove elements to construct new branch
            for( size_t i=branch.size()-1; i>0; i-- ) {
                if( branch[i]->possibleChildren.size() == 0 )
                    branch.pop_back();
                else
                    break;
            }

and i think this only removes the element from vector but keeps the allocated space by new inside the createBranch function.

then i started using
1
2
3
4
5
6
7
8
9
        // Remove elements to construct new branch
            for( size_t i=branch.size()-1; i>0; i-- ) {
                if( branch[i]->possibleChildren.size() == 0 ) {
                    delete &branch[branch.size()-1];
                    branch.pop_back();
                }
                else
                    break;
            }

which supposedly would also delete the allocated space but i get random errors in run time. (sometimes it gives an error and crashes the program, others it works)

Are you suggesting i change this to:
1
2
3
4
5
6
7
8
9
        // Remove elements to construct new branch
            for( size_t i=branch.size()-1; i>0; i-- ) {
                if( branch[i]->possibleChildren.size() == 0 ) {
                    branch[i] = NULL;
                    branch.pop_back();
                }
                else
                    break;
            }

??
Last edited on
delete &branch[branch.size()-1];

I think you're getting confused.

branch[branch.size()-1] would give you the pointer to the memory you allocated. This, you can (and should, if you want to destroy that object) delete.

&branch[branch.size()-1]; gives you a pointer into the memory that the vector uses to store the pointers you're pushing into it. The vector class manages its own memory - you should not be attempting to delete it.
Last edited on
thanks for the explanation MikeyBoy!
i am mostly confused, that's for sure! ahahah

well with
delete &branch[branch.size()-1];
and
delete branch[branch.size()-1];
i get the exact same type of output. it crashes the program ("program.exe has stopped working")

with
branch[i] = NULL
seems to work fine!

so +1 ajh32!! ( i will test it further to see if it really works 100% of the times!) ahah

just for the sake of argument what's the difference between

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
for( size_t i=branch.size()-1; i>0; i-- ) {
    if( branch[i]->possibleChildren.size() == 0 )
        branch.pop_back();
    else
        break;
}

// OR 

for( size_t i=branch.size()-1; i>0; i-- ) {
    if( branch[i]->possibleChildren.size() == 0 ) {
        delete branch[branch.size()-1];
        branch.pop_back();
    }
    else
        break;
}

// OR

for( size_t i=branch.size()-1; i>0; i-- ) {
    if( branch[i]->possibleChildren.size() == 0 ) {
        branch[i] = NULL;
        branch.pop_back();
    }
    else
        break;
}


for this particular case (where branch is a vector<tNode*> and filled with branch.push_back(new tNode(//...)) )? What are the results/consequences of each and when to use one or the other (assuming all of them are correct ways of doing something)?
Last edited on
You are trying to modify the vector, and same time using the size operator. I don't think we can do like that

branch.push_back( new tNode(branch[branch.size()-1]->possibleChildren[branch[branch.size()-1]->possibleChildren.size()-1]) );
well actually i don't see why i shouldn't (and it works just fine by the way)...

i agree it would be a lot more legible to declare a variable and then use it. but the code is definitly not wrong and i don't think i'm using any "bad habits" or bad practices here... if i'm wrong please do tell me!
Topic archived. No new replies allowed.