C/C++
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs C/C++

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 6th, 2008, 01:00 PM
pereges
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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 ?

Reply With Quote
  #2  
Old July 6th, 2008, 01:39 PM
Joe Wright
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #3  
Old July 6th, 2008, 01:39 PM
pereges
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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.

Reply With Quote
  #4  
Old July 6th, 2008, 09:39 PM
santosh
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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.


Reply With Quote
Reply

Viewing: Web Development Archives FAQs C/C++ > Unable to deallocate memory for a tree structure.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway