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.










share|improve this question




















  • 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 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






  • 1




    @Arnil You were saying?
    – melpomene
    Nov 17 at 14:07















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.










share|improve this question




















  • 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 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






  • 1




    @Arnil You were saying?
    – melpomene
    Nov 17 at 14:07













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.










share|improve this question















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++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 13:33









melpomene

56.2k54387




56.2k54387










asked Nov 17 at 13:31









Arnil

41




41








  • 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 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






  • 1




    @Arnil You were saying?
    – melpomene
    Nov 17 at 14:07














  • 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 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






  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno