-1
我有一個自定義struct
mystruct。使用valgrind在c中丟失內存
typedef struct mystruct
{
int data;
struct mystruct * parent;
struct mystruct * child;
struct mystruct * next;
}mystruct;
現在我做這個MYSTRUCT的後序遍歷的功能traverse()
mystruct * create(mystruct * root)
{
mystruct * newNode=malloc(sizeof(mystruct));
//change some pointers like make newNode->parent=root->parent
//
//
return newNode;
}
void traverse(mystruct * root)
{
if(root==NULL)
return;
//here I am calling a new function
if() // somecondition
{
mystruct * newNode=create(root);
root=NULL;
free(root);
root=newNode;
}
traverse(root->child);
traverse(root->next);
}
void delete(mystruct * root)
{
if(root==NULL)
return;
delete(root->child);
delete(root->next);
free(root);
}
即使在年底釋放我的結構之後,valgrind
顯示由於newNode
記憶力減退創建。我如何消除這些內存損失?