P
pereges
Hi, I'm trying to deallocate a kd tree which I created dynamically.
There were no problems
creating the structure and I can access it easily but there is a
problem while trying to free
it. Here's the structure for the a single node in the tree:
typedef struct kdnode_s
{
bbox box; /* Bounding box */
int splitplane; /* -1 for leaf node*/
union
{
struct
{
struct kdnode_s *child[2]; /* Children node */
double split;
}nonleaf;
struct
{
size_t nt;
node *thead; /* Link list */
}leaf;
}info;
}kdnode;
Some other supporting data structures :
/* vector */
typedef struct
{
double coord[3];
}vector;
/* bounding box */
typedef struct
{
vector maxB, minB;
}bbox;
/* link list node */
typedef struct node_s
{
struct node_s *next;
void *data;
}node;
Heres the function I wrote to free the kdtree. I pass the address of
the root node pointer
and traverse the tree until I reach a leaf node which is characterised
by splitplane member
being -1. If it is not -1, then it has values 0,1,2 and it is a
nonleaf node. The leaf node
is freed and then I try to free other nodes as I climb back. Once a
leaf node is freed its
pointer is made NULL. This makes it easier to free other nodes once
their children have
been freed.
void free_kd_tree(kdnode **kd)
{
if (*kd != NULL)
{
if ((*kd)->splitplane == -1)
{
free_list((*kd)->info.leaf.thead);
*kd = NULL;
return ;
}
else
{
free_kd_tree(&(*kd)->info.nonleaf.child[0]);
free_kd_tree(&(*kd)->info.nonleaf.child[1]);
if ((*kd)->info.nonleaf.child[0] == NULL &&
(*kd)->info.nonleaf.child[1] == NULL)
{
free(*kd);
*kd = NULL;
}
}
}
}
In this program the free_list is a routine to free the link list given
its head pointer and this
is how I wrote it:
void free_list(node *head)
{
node *p;
p = head;
while (p != NULL)
{
free(p);
p = p->next;
}
}
For some reason I observed, the free(p) is not able to execute and the
program terminates
at the point without any warning or message. What could be wrong ?
There were no problems
creating the structure and I can access it easily but there is a
problem while trying to free
it. Here's the structure for the a single node in the tree:
typedef struct kdnode_s
{
bbox box; /* Bounding box */
int splitplane; /* -1 for leaf node*/
union
{
struct
{
struct kdnode_s *child[2]; /* Children node */
double split;
}nonleaf;
struct
{
size_t nt;
node *thead; /* Link list */
}leaf;
}info;
}kdnode;
Some other supporting data structures :
/* vector */
typedef struct
{
double coord[3];
}vector;
/* bounding box */
typedef struct
{
vector maxB, minB;
}bbox;
/* link list node */
typedef struct node_s
{
struct node_s *next;
void *data;
}node;
Heres the function I wrote to free the kdtree. I pass the address of
the root node pointer
and traverse the tree until I reach a leaf node which is characterised
by splitplane member
being -1. If it is not -1, then it has values 0,1,2 and it is a
nonleaf node. The leaf node
is freed and then I try to free other nodes as I climb back. Once a
leaf node is freed its
pointer is made NULL. This makes it easier to free other nodes once
their children have
been freed.
void free_kd_tree(kdnode **kd)
{
if (*kd != NULL)
{
if ((*kd)->splitplane == -1)
{
free_list((*kd)->info.leaf.thead);
*kd = NULL;
return ;
}
else
{
free_kd_tree(&(*kd)->info.nonleaf.child[0]);
free_kd_tree(&(*kd)->info.nonleaf.child[1]);
if ((*kd)->info.nonleaf.child[0] == NULL &&
(*kd)->info.nonleaf.child[1] == NULL)
{
free(*kd);
*kd = NULL;
}
}
}
}
In this program the free_list is a routine to free the link list given
its head pointer and this
is how I wrote it:
void free_list(node *head)
{
node *p;
p = head;
while (p != NULL)
{
free(p);
p = p->next;
}
}
For some reason I observed, the free(p) is not able to execute and the
program terminates
at the point without any warning or message. What could be wrong ?