parent pointer will not be set in A*

I atempting to set the parent pointer of node, and then push that node into a vector.. But every time I push into that vector the pointer to the parent becomes null...

How come??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct node
{
    node() = default;
    node(const state_s &obj)
    {
        this->man = obj.man;
        this->heuristic = obj.heuristic;
        this->cost = obj.cost;
    }

    position_t man;
    char heuristic = 0, cost = 0;
    node *parent;

};


Here is the node declaration..
In my A* algorithm i am attempting to push the changed node into a open list (being a priority queue).
1
2
3
4
5
6
7
8
9
10
11
        parent =  open.top();
        open.pop();
        ...some stuff that retrieves the child, and checks whether if already exist in open or closed with an lower cost....
        if(/*child of the parent is not in the closed list*/)
            {
                child.cost = cost;
                child.heuristic = child.cost + get_heuristic(child);
                child.parent = &parent;
                open.push(child);

            }



When i perform the push with the child.. the parent becomes zero within the open list.. Why.. and how do overcome this issue?




Last edited on
When i perform the push with the child.. the parent becomes zero within the open list


It isn't obvious from your code (which seems to indicate parent will not be in the open list) or your explanation of the problem what is happening. More context is required.

I suspect, since you seem to be copying nodes instead of keeping them in once place, that your pointers are invalidated at some point.
I am not sure what form of information you need.. Could you clarify where form of context is needed.. I am trying to limit myself here, instead posting a wall of codes..
I am trying to limit myself here, instead posting a wall of codes..

Always appreciated. Distilling the code to the smallest example that exhibits the problem you're experiencing is usually recommended.

But let me revisit what I said before:
I suspect, since you seem to be copying nodes instead of keeping them in once place, that your pointers are invalidated at some point.


On line 1 of your second code snippet above, you copy a variable in a container to the variable parent. On line 8 you set the parent pointer of a node to the address of the parent object manipulated earlier.

Now, if the function this code occurs in terminates, the parent variable is destroyed and the parent pointer of the node in your container will point to an invalid object. If this is part of a loop and parent is reassigned a value on another iteration of the loop, the parent pointer of the node in your container will point to a node that is not it's parent.

This is the sort of context I'm talking about.
Last edited on
Topic archived. No new replies allowed.