I am trying to implement a tree structure and I am storing pointers to children nodes in an array. I am trying to add nodes but they aren't working as expected. For example, somewhere in the code the pointer gets the value 0xfeeefeeefeeefeee and I encounter a segmentation fault. I am not too familiar with C++ pointers and memory allocation. Can someone tell me what is the issue?
Code:
#include <iostream>
#include <vector>
using namespace std;
class Node{
public:
vector <Node*> children = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
};
Node parent;
/*
void changePointer(Node **a, Node* b){
*a = b;
}
*/
void add_to_visited(vector <vector <vector<int>>> arr){
Node* curr = &parent;
for (int face=0;face<6;face++){
for (int row=0;row<2;row++){
for (int col=0;col<2;col++){
if (curr->children[arr[face][row][col]]==nullptr){
Node temp;
curr->children[arr[face][row][col]]=&temp;
}
curr = curr->children[arr[face][row][col]];
//changePointer(&curr, curr->children[arr[face][row][col]]);
}
}
}
}
int main(){
vector <vector <vector <int>>> arr =
{
{
{4, 1},
{2, 4}
},
{
{5, 3},
{5, 4}
},
{
{2, 0},
{1, 5}
},
{
{0, 1},
{2, 0}
},
{
{1, 3},
{3, 0}
},
{
{5, 2},
{3, 4}
}
};
add_to_visited(arr);
}