problem in freeing a "tree" of stuctures
let me start saying that I'm not a good c programmer
anyway at the moment I'm working on a program that someone else has written and that I have to modify to add other features (it deals with electromagnetic engineering but it is not relevant I guess)
DBimages is the following structure:
Code:
typedef struct DBIMAGES
{
int *face;
double *point;
int Nbfaces;
struct DBimages *NextLevel;
struct DBimages *NextSameLevel;
struct DBimages *PreviousLevel;
} DBimages;
as you can imagine the pointers inside the structure points to other structures to build a sort of tree with different levels
in the original program a sort of tree was built only once and it was used till the end of the program
that's why I think they didn't bother to write something to free that
now I have to build several of those and I'm running short of memory without freeing
this is what I tried to write (maxlev is the height of the tree or better the maximum number of levels allowed in the tree):
Code:
void FreeDBimages (ptr,maxlev)
DBimages *ptr;
int maxlev;
{
DBimages *curs,*temp;
int level;
level=1;
curs=(DBimages*)ptr->NextLevel;
while(level<maxlev){
curs=(DBimages*)curs->NextLevel;
level++;
}
while(curs!=NULL){
temp=curs;
FreedVector(temp->point,3);
FreeiVector(temp->face,2);
if(curs->NextSameLevel!=NULL)
curs=(DBimages*)curs->NextSameLevel;
else
curs=(DBimages*)curs->PreviousLevel;
free(temp);
}
}
unfortunately this function seems not to work
the first time I run it I got a segmentation fault (quite far from the beginning of the while loop)
anyone has any remark or suggestion or correction?
thanks in advance for any help