Strange Behavior with pointers

  • I have run into something sort of odd. I will do my best to explain. I created a simple (sparsely populated) graph. The code for the graph node (and everything else) below. All graph nodes and edges are custom class objects and are stored in the graph as pointers. Graph Nodes inherit from a base class called Node.

    Ok. i have written the skeleton of a path finding routine. When the path finding routine starts, it checks to see if the starting node is not the ending node and, if not, it loads a pointer to the graph node into a binary heap.

    My issue is this, when I examine the nodes in the binary heap, some of the data is there and some of the data is not. I've examined the graph nodes and found that before the path finding starts all of the data is present. Specifically, I'm missing the names of the GraphNodes.

    class Node : public QObject

    //Member variables
    QString m_sNodeName;
    int m_iNodeHeuristic;
    int m_iIndex;

    explicit Node(QObject *parent = 0);
    Node(QString name, int heuristic, int index);

    QString Name();
    void SetName(QString name);
    int Heuristic();
    void SetHeuristic(int heuristic);
    int Index();
    void SetIndex(int index);
    bool operator==(const Node &rhs) const;
    bool operator<=(const Node &rhs) const;
    bool operator>=(const Node &rhs) const;


    public slots:



    //Graph node code.

    class GraphNode : public Node

    QVector<Edge*> m_edgeList;
    //Private methods
    bool EdgeExists(int from, int to, double cost);

    GraphNode(QString name, int heuristic, int Index);

    //Public methods
    void AddEdge(const int from, const int to, const double distance);
    void AddEdge(const int from, const int to, const double distance, const double cost);
    Edge *GetEdge(int from, int to);
    QVector<Edge*> Edges();
    //Overloaded Operators.
    bool operator==(const GraphNode &rhs) const;
    bool operator>=(const GraphNode &rhs) const;
    bool operator<=(const GraphNode &rhs) const;


    PathNode Code: This node is loaded into a QVector<PathNode*> containers

    class PathNode

    PathNode* m_pnpParent;
    GraphNode* m_gnpNode;
    double m_dFCost;
    double m_dTotalCost;
    bool m_bClosed;
    bool m_bOpen;

    PathNode(GraphNode &node, double fcost, double totalcost);
    PathNode(PathNode &parent, GraphNode &node, double fcost, double totalcost);

    bool Closed();
    void SetClosed(bool newvalue);
    bool Open();
    void SetOpen(bool newvalue);
    double FCost();
    double TotalCost();
    PathNode* Parent();
    GraphNode* Node();
    bool operator==(const PathNode &rhs) const;
    bool operator<=(const PathNode &rhs) const;
    bool operator<(const PathNode &rhs) const;
    bool operator>=(const PathNode &rhs) const;
    bool operator>(const PathNode &rhs) const;


    //In the path finder ... here is where I acquire a reference to the graph node and move it to the open list
    qDebug() << "Creating the starting path node ....";
    //Create the starting path node and add it to the Open List
    node = m_gGraphMap->GetGraphNode(start);
    currNode = new PathNode(*node, 0.0, 0.0);

        //Add the starting node to both the Open List and the Master List
        m_mapMasterList.insert(start, *currNode);


  • Hi cazador7907,

    and here is your problem exactly?
    In this code _ only see one thing:
    m_mapMasterList contains copys of the objects and m_bhOpenList contains the object pointers itself. So the objects of these to list may differ and ill definitly not be the same.

  • I am not actually sure what I was doing wrong. However, I cut out the code in the path finder and started rebuilding it. For whatever reason all of the data in the objects seems to be there now.

    I am curious about one thing, you mentioned that I am storing an actually copy of the pathnode object in the map. However, when the command

    m_mapMasterList.insert(start, *currNode)

    shouldn't that copy the pointer to the map or should I use

    m_mapMasterList.insert(start, &currNode)?

    I've been reading up on pointers but it's definitely something requires a lot of time and can lead to really serious disasters.

  • so, this is C++ basics:

    PathNode* currNode;
    currNode = new PathNode(*node, 0.0, 0.0);

    currNode is the pointer.


    dereferences the pointer and returns a PathNode object (reference).


    gives you the address of the pointer, so a PathNode**

    If you want to store the pointer in the map, use the pointer as in the list:

    PathNode* currNode;
    currNode = new PathNode(*node, 0.0, 0.0);
    m_mapMasterList.insert(start, currNode);

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.