Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Strange Behavior with pointers
QtWS25 Last Chance

Strange Behavior with pointers

Scheduled Pinned Locked Moved C++ Gurus
4 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cazador7907
    wrote on last edited by
    #1

    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
    {
    Q_OBJECT

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

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

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

    signals:

    public slots:

    };
    @

    @

    //Graph node code.

    class GraphNode : public Node
    {
    private:

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

    public:
    //Ctor
    GraphNode();
    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
    {

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

    public:
    PathNode();
    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);
    currNode->SetClosed(false);
    currNode->SetOpen(true);

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

    @

    Laurence -

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cazador7907
        wrote on last edited by
        #3

        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.

        Laurence -

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          so, this is C++ basics:

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

          currNode is the pointer.

          @
          *currNode;
          @

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

          @
          &currNode;
          @

          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_bhOpenList.Push(currNode);
          m_mapMasterList.insert(start, currNode);
          @

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved