Reputation: 191
void node::assign_childs()
{
node *childs;
childs = new node[8];
.
.
.
child_pointer = &childs[0];
}
I have trouble to find a way to free the memory of "child_pointer". "child_pointer" is defined in the the struct "node" as shown below.
struct node{
vect p;
vect norm;
float s;
unsigned char r,g,b;
int fac_c;
vector<int> cfac;
bool isParent;
bool filled;
struct node* child_pointer;
node(vect, float , vect );
~node();
void assign_childs();
};
Using delete to free child_pointer does not appear to work, i have also tried delete [].
How could i create a destructor to free all child_pointers recursively down from the root node?
(this is a octree, so when i free a node all nodes within that node also gets freed)
if i have to change the way i assign/define child_pointer to make it possible to free it. How would i do that? using an array of 8 pointers is not a alternative, as it will use way to much memory.
here is the destructor i have tried.
node::~node(){
if (child_pointer != NULL){
delete [] child_pointer;
}
}
doing this will crash my program with a segmentation fault.
node *octree(...);
.
.
.
delete octree;
Upvotes: 0
Views: 2210
Reputation: 2273
Just add the following in your destructor:
delete[] child_pointer;
Delete will call the destructor of the children, their deletes will call the destructors of the children's children etc.
Just showing how delete and delete[] is used:
int *foo = new int;
delete foo;
int *bar = new int[42];
delete[] bar;
Upvotes: 3