|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Unable to deallocate memory for a tree structure.
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. 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 ? |
|
#2
|
|||
|
|||
|
Unable to deallocate memory for a tree structure.
pereges wrote:
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. 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 ? p is freed ('free(p);') what do you think 'p->next' means? -- Joe Wright "Everything should be made as simple as possible, but not simpler." Albert Einstein |
|
#3
|
|||
|
|||
|
Unable to deallocate memory for a tree structure.
Unfotunately, my pelles C compiler never reports such dangerous
situations. Can some one please tell me of some easy to use debugger in windows mode which will give warnings and stuff ? I know valegrind is a good one but its for linux. Pelles C has a debugger but it produces enormous amount of illegible assembly code and various stack information. I feel extremely frustrated because of these bugs. |
|
#4
|
|||
|
|||
|
Unable to deallocate memory for a tree structure.
pereges wrote:
Unfotunately, my pelles C compiler never reports such dangerous situations. Can some one please tell me of some easy to use debugger in windows mode which will give warnings and stuff ? I know valegrind is a good one but its for linux. Pelles C has a debugger but it produces enormous amount of illegible assembly code and various stack information. I feel extremely frustrated because of these bugs. Have you tried static code checking tools like PC-Lint or splint? They would've caught the mistake you made. |
![]() |
| Viewing: Web Development Archives > FAQs > C/C++ > Unable to deallocate memory for a tree structure. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|