如何释放内部有结构单元格的链表?

how to free a linked-list that has structures cells inside?

我想知道应该释放分配给链表的内存的抑制函数出了什么问题。

这是我的代码:

typedef struct cell {
int number;
struct cell *next;}cell;

typedef struct {
int sign; //1 or 0
int nbrCell ;
cell *numbers;} num ;

初始化函数:

num *initialisation(){

num *num = malloc(sizeof(*num));
cell *cell =malloc(sizeof(*cell));

num->numbers = cell;
cell->next=NULL;
num->sign= 0 ;
num->nbrCell =0;
return num ;}

删除程序

void suppression(num* num){

  cell* Delete;
  cell* newC = num->numbers;

   while (newC)
   {
   Delete = newC;
   newC = newC->next;
   free(Delete);
       }
           } 

现在在我的 Main 我有

int main(){
num* number;
number=initialisation();
suppression(number);
 return 0;
  }

当我尝试 valgrind 时,它说:

==21281== HEAP SUMMARY:
==21281==     in use at exit: 16 bytes in 1 blocks
==21281==   total heap usage: 2 allocs, 1 frees, 32 bytes allocated
==21281== 
==21281== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1
==21281==    at 0x4C2AB80: malloc (in /usr/lib/valgrind  /vgpreload_memcheck-amd64-linux.so)
==21281==    by 0x4005CE: initialisation (test.c:26)
 ==21281==    by 0x4006D5: main (test.c:85)
 ==21281== 
 ==21281== LEAK SUMMARY:
 ==21281==    definitely lost: 16 bytes in 1 blocks
 ==21281==    indirectly lost: 0 bytes in 0 blocks
 ==21281==      possibly lost: 0 bytes in 0 blocks
 ==21281==    still reachable: 0 bytes in 0 blocks
 ==21281==         suppressed: 0 bytes in 0 blocks
 ==21281== 
 ==21281== For counts of detected and suppressed errors, rerun with: -v
 ==21281== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

甚至当我尝试插入一些数字时,我总是得到额外的 1 个分配,例如 5 个分配,4 个释放..

在我的项目中我使用了数字结构来保存数字

喜欢 1->2->3->4->5->6->NULL 2 是 num->numbers->next 等..

每次我添加一个数字时,它都会被放入一个单元格中,然后我将该单元格添加到数字结构中。 现在我想知道如何释放 num 并且当然释放它拥有的所有单元格..

插入函数

  void insertion(num *number, int element ){

  cell *cellule = malloc(sizeof(*cellule));
  if (number == NULL || cellule == NULL)
  {
   exit(-1);
   }
   cellule->number = element;


       cellule->next = number->numbers;
       number->numbers = cellule;
       number->nbrCell++ ;

         }

如果这不起作用,则说明您的列表有问题,否则问题来自 num->nbrCell

void suppression(num* num){

   cell* Delete;
   cell* newC = num->numbers;

   while (newC) // != NULL is implicit
   {
       Delete = newC;
       newC = newC->next;
       free(Delete);
   }
}