Pushed back reference to pointer list causing issues
up vote
-1
down vote
favorite
Pardon me if a similar question was asked before, I've tried hard searching for an answer but to no avail.
So, I was making a Tic-Tac-Toe Monte Carlo Tree Search algorithm. The Node struct required to have a list because incomplete types cannot be used inside a struct. But, I faced a major problem when setting the parent of a newly created Child Node.
What I did is that I first created a ChildNode, set the CurrentNode as its Parent Node and then did some things with the Child Node. Then I pushed back the memory address of the Child Node to the list containing pointers to the child nodes of the Current Node. The problem is, every time the ChildNode is modified (to add more different Child Nodes) even after its reference is pushed back into the list, the value pointed to inside the list changes as well. Which causes problems in my code.
The code below is not the actual code, but it is made to reflect what my problem actually is.
Tree tree;
Node* CurrentNode = &(tree.root);
Node ChildNode;
for(int i = 0; i < 6; i++)
{
ChildNode.parent = CurrentNode;
//do things to Child Node
CurrentNode->childs.push_back(&ChildNode);
CurrentNode = CurrentNode->childs.back();
}
I hope it can be solved easily without hassle.
c++
|
show 6 more comments
up vote
-1
down vote
favorite
Pardon me if a similar question was asked before, I've tried hard searching for an answer but to no avail.
So, I was making a Tic-Tac-Toe Monte Carlo Tree Search algorithm. The Node struct required to have a list because incomplete types cannot be used inside a struct. But, I faced a major problem when setting the parent of a newly created Child Node.
What I did is that I first created a ChildNode, set the CurrentNode as its Parent Node and then did some things with the Child Node. Then I pushed back the memory address of the Child Node to the list containing pointers to the child nodes of the Current Node. The problem is, every time the ChildNode is modified (to add more different Child Nodes) even after its reference is pushed back into the list, the value pointed to inside the list changes as well. Which causes problems in my code.
The code below is not the actual code, but it is made to reflect what my problem actually is.
Tree tree;
Node* CurrentNode = &(tree.root);
Node ChildNode;
for(int i = 0; i < 6; i++)
{
ChildNode.parent = CurrentNode;
//do things to Child Node
CurrentNode->childs.push_back(&ChildNode);
CurrentNode = CurrentNode->childs.back();
}
I hope it can be solved easily without hassle.
c++
2
IfNode ChildNode;is a local variable, it goes out of scope when the function ends. Can a local variable's memory be accessed outside its scope?
– Johnny Mopp
Nov 17 at 13:37
Would the pointer and value pointed to still remain inside the list, though?
– Arnil
Nov 17 at 13:38
1
Thechildsmember ofNodeshould probably contain nodes themselves rather than pointers to nodes e.g. you seem to havestd::list<Node*>, and every one of those pointers points to the same variableChildNode. Change it to something likestd::list<Node>.
– hegel5000
Nov 17 at 13:45
C++ doesn't allow to use an incomplete type inside a struct. I can't declare a list<Node> inside the Node struct
– Arnil
Nov 17 at 13:48
1
@Arnil You were saying?
– melpomene
Nov 17 at 14:07
|
show 6 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Pardon me if a similar question was asked before, I've tried hard searching for an answer but to no avail.
So, I was making a Tic-Tac-Toe Monte Carlo Tree Search algorithm. The Node struct required to have a list because incomplete types cannot be used inside a struct. But, I faced a major problem when setting the parent of a newly created Child Node.
What I did is that I first created a ChildNode, set the CurrentNode as its Parent Node and then did some things with the Child Node. Then I pushed back the memory address of the Child Node to the list containing pointers to the child nodes of the Current Node. The problem is, every time the ChildNode is modified (to add more different Child Nodes) even after its reference is pushed back into the list, the value pointed to inside the list changes as well. Which causes problems in my code.
The code below is not the actual code, but it is made to reflect what my problem actually is.
Tree tree;
Node* CurrentNode = &(tree.root);
Node ChildNode;
for(int i = 0; i < 6; i++)
{
ChildNode.parent = CurrentNode;
//do things to Child Node
CurrentNode->childs.push_back(&ChildNode);
CurrentNode = CurrentNode->childs.back();
}
I hope it can be solved easily without hassle.
c++
Pardon me if a similar question was asked before, I've tried hard searching for an answer but to no avail.
So, I was making a Tic-Tac-Toe Monte Carlo Tree Search algorithm. The Node struct required to have a list because incomplete types cannot be used inside a struct. But, I faced a major problem when setting the parent of a newly created Child Node.
What I did is that I first created a ChildNode, set the CurrentNode as its Parent Node and then did some things with the Child Node. Then I pushed back the memory address of the Child Node to the list containing pointers to the child nodes of the Current Node. The problem is, every time the ChildNode is modified (to add more different Child Nodes) even after its reference is pushed back into the list, the value pointed to inside the list changes as well. Which causes problems in my code.
The code below is not the actual code, but it is made to reflect what my problem actually is.
Tree tree;
Node* CurrentNode = &(tree.root);
Node ChildNode;
for(int i = 0; i < 6; i++)
{
ChildNode.parent = CurrentNode;
//do things to Child Node
CurrentNode->childs.push_back(&ChildNode);
CurrentNode = CurrentNode->childs.back();
}
I hope it can be solved easily without hassle.
c++
c++
edited Nov 17 at 13:33
melpomene
56.2k54387
56.2k54387
asked Nov 17 at 13:31
Arnil
41
41
2
IfNode ChildNode;is a local variable, it goes out of scope when the function ends. Can a local variable's memory be accessed outside its scope?
– Johnny Mopp
Nov 17 at 13:37
Would the pointer and value pointed to still remain inside the list, though?
– Arnil
Nov 17 at 13:38
1
Thechildsmember ofNodeshould probably contain nodes themselves rather than pointers to nodes e.g. you seem to havestd::list<Node*>, and every one of those pointers points to the same variableChildNode. Change it to something likestd::list<Node>.
– hegel5000
Nov 17 at 13:45
C++ doesn't allow to use an incomplete type inside a struct. I can't declare a list<Node> inside the Node struct
– Arnil
Nov 17 at 13:48
1
@Arnil You were saying?
– melpomene
Nov 17 at 14:07
|
show 6 more comments
2
IfNode ChildNode;is a local variable, it goes out of scope when the function ends. Can a local variable's memory be accessed outside its scope?
– Johnny Mopp
Nov 17 at 13:37
Would the pointer and value pointed to still remain inside the list, though?
– Arnil
Nov 17 at 13:38
1
Thechildsmember ofNodeshould probably contain nodes themselves rather than pointers to nodes e.g. you seem to havestd::list<Node*>, and every one of those pointers points to the same variableChildNode. Change it to something likestd::list<Node>.
– hegel5000
Nov 17 at 13:45
C++ doesn't allow to use an incomplete type inside a struct. I can't declare a list<Node> inside the Node struct
– Arnil
Nov 17 at 13:48
1
@Arnil You were saying?
– melpomene
Nov 17 at 14:07
2
2
If
Node ChildNode; is a local variable, it goes out of scope when the function ends. Can a local variable's memory be accessed outside its scope?– Johnny Mopp
Nov 17 at 13:37
If
Node ChildNode; is a local variable, it goes out of scope when the function ends. Can a local variable's memory be accessed outside its scope?– Johnny Mopp
Nov 17 at 13:37
Would the pointer and value pointed to still remain inside the list, though?
– Arnil
Nov 17 at 13:38
Would the pointer and value pointed to still remain inside the list, though?
– Arnil
Nov 17 at 13:38
1
1
The
childs member of Node should probably contain nodes themselves rather than pointers to nodes e.g. you seem to have std::list<Node*>, and every one of those pointers points to the same variable ChildNode. Change it to something like std::list<Node>.– hegel5000
Nov 17 at 13:45
The
childs member of Node should probably contain nodes themselves rather than pointers to nodes e.g. you seem to have std::list<Node*>, and every one of those pointers points to the same variable ChildNode. Change it to something like std::list<Node>.– hegel5000
Nov 17 at 13:45
C++ doesn't allow to use an incomplete type inside a struct. I can't declare a list<Node> inside the Node struct
– Arnil
Nov 17 at 13:48
C++ doesn't allow to use an incomplete type inside a struct. I can't declare a list<Node> inside the Node struct
– Arnil
Nov 17 at 13:48
1
1
@Arnil You were saying?
– melpomene
Nov 17 at 14:07
@Arnil You were saying?
– melpomene
Nov 17 at 14:07
|
show 6 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351715%2fpushed-back-reference-to-pointer-list-causing-issues%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
If
Node ChildNode;is a local variable, it goes out of scope when the function ends. Can a local variable's memory be accessed outside its scope?– Johnny Mopp
Nov 17 at 13:37
Would the pointer and value pointed to still remain inside the list, though?
– Arnil
Nov 17 at 13:38
1
The
childsmember ofNodeshould probably contain nodes themselves rather than pointers to nodes e.g. you seem to havestd::list<Node*>, and every one of those pointers points to the same variableChildNode. Change it to something likestd::list<Node>.– hegel5000
Nov 17 at 13:45
C++ doesn't allow to use an incomplete type inside a struct. I can't declare a list<Node> inside the Node struct
– Arnil
Nov 17 at 13:48
1
@Arnil You were saying?
– melpomene
Nov 17 at 14:07