Pointer problems: program randomly hangs.

Aug 30, 2014 at 10:41am
I'm trying to write a generic doubly-linked tree (or some variation of that, i guess)

This is the code for adding a node to another already present node.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Node<T>* addNode(Node<T>* linkTo)
{
   //Create new node, add it to total nodes.
   Node<T> newNode;
   nodes.push_back(newNode);
   //Create a pointer to back of the vector.
   Node<T> *newPointer;
   newPointer = &nodes.back();
   //Set previous node to parameter pointer
   newPointer->previous.push_back(linkTo);
   //Set parameter pointer's next to our node
   linkTo->next.push_back(newPointer);
   return newPointer;
}


In the main() function:
1
2
3
4
5
Tree<int> a;
//createRoot() is the same as addNode(), except it doesnt take arguments
//and doesnt link any nodes together
Node<int> *pointerlel = a.createRoot(); 
Node<int> *anthrptrlel = a.addNode(pointerlel);

Sometimes, it will work. Sometimes it randomly hangs forever. Any idea what might be wrong?
Additional notes: If i delete the line containing addNode(), or createRoot(), the program doesn't hang randomly anymore. They are mutually exclusive apparently.

Also, should I be using smart pointers or something?
Aug 30, 2014 at 10:48am
What type is nodes?
Aug 30, 2014 at 10:57am
Here's the full class:

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
29
30
31
32
33
34
35
36
37
38
39
#ifndef TREE_H
#define TREE_H

#include "Node.h"

template <class T>
class Tree
{
    public:
        vector<Node <T> > nodes;

        Node<T>* createRoot()
        {
            //Create a new, empty node, and add it to the node vector.
            Node<T> newNode;
            nodes.push_back(newNode);
            //Return a pointer that points to the area in mmry in vector;
            Node<T> *newPointer;
            newPointer = &nodes.back();
            return newPointer;
        }

        Node<T>* addNode(Node<T>* linkTo)
        {
            //Create new node, add it to total nodes.
            Node<T> newNode;
            nodes.push_back(newNode);
            //Create a pointer to back of the vector.
            Node<T> *newPointer;
            newPointer = &nodes.back();
            //Set previous node to parameter pointer
            newPointer->previous.push_back(linkTo);
            //Set parameter pointer's next to our node
            linkTo->next.push_back(newPointer);
            return newPointer;
        }
};

#endif // TREE_H 
Aug 30, 2014 at 11:03am
A vector invalidates iterators and/or pointers to elements when it needs to expand, so the address obtained on line 30 may become invalid later in the program.
Aug 30, 2014 at 11:10am
I understand what you mean here. This is most likely the cause of the problem. How do you propose I keep the pointers to the nodes, then?

edit: Will it still invalidate it if I use a list instead?
Last edited on Aug 30, 2014 at 11:14am
Aug 30, 2014 at 11:16am
A std::deque wouldn't suffer from that particular issue.

(Iterators may still be invalidated when the container needs to expand, but pointers wouldn't be.)
Last edited on Aug 30, 2014 at 11:17am
Aug 30, 2014 at 11:19am
Thank you very much!
Topic archived. No new replies allowed.