如何修复C程序中的内存泄漏?

如何修复C程序中的内存泄漏?,c,struct,memory-leaks,valgrind,singly-linked-list,C,Struct,Memory Leaks,Valgrind,Singly Linked List,下面的C程序应该分配一个列表,然后取消分配并同时打印其内容: #include <stdlib.h> #include <stdio.h> typedef struct { struct chain *next; int contents; } chain; int main() { int index; chain *list; chain *p; chain *pointer;

下面的C程序应该分配一个列表,然后取消分配并同时打印其内容:

#include <stdlib.h>
#include <stdio.h>
typedef struct {
struct chain *next;       
int contents;  } chain; 

int main()  {     
    int index;     
    chain *list;     
    chain *p;    
    chain *pointer;     
    list = malloc(sizeof(chain));     
    p = list;     

    for(index=0;index<10;index++) {       
        (*p).contents = index;       
        (*p).next = malloc(sizeof(chain));       
        p = (*p).next; 
       } ;     
    p = pointer = list;     
    index = 0;     

    while (index < 9) { 
        printf("cell # %d: %d\n",index,(*p).contents);       
        p = (*p).next;       
        free(pointer);       
        pointer = p;       
        index++; 
      } ;     
    printf("First cell: %d\n",(*list).contents); 
    return 0;  
  } 
我是一个完全的C语言新手,我不知道这些漏洞是从哪里来的,我需要做些什么来纠正它们。非常感谢你的帮助

这个声明

typedef struct {
struct chain *next;       
int contents;  } chain; 
这是不正确的。有两种结构。第一个是未命名结构

typedef struct {
为其分配了typedef名称链。在这个未命名的结构中,声明了一个或多个结构struct chain,用作下一个数据成员的说明符

所以chain*和struct chain*类型的指针不兼容

结构必须声明为

typedef struct chain {
    struct chain *next;       
    int contents;  
} chain; 
在这个代码片段中

list = malloc(sizeof(chain));     
p = list;     

for(index=0;index<10;index++) {       
    (*p).contents = index;       
    (*p).next = malloc(sizeof(chain));       
    p = (*p).next; 
   } ;  
例如,可以用以下方法编写程序

#include <stdlib.h>
#include <stdio.h>

typedef struct chain
{
    struct chain *next;       
    int contents;  
} chain; 

int main( void )  
{
    const int N = 10;

    chain *list = NULL;

    chain **current = &list;

    for ( int i = 0; i < N; i++ ) 
    {
        *current = malloc( sizeof( chain ) );
        ( *current )->contents = i;       
        ( *current )->next = NULL;       
        current = &( *current )->next;
    } ;     

    for ( int i = 0; i < N - 1; i++ )
    {
        printf( "cell # %d: %d\n", i,  list->contents ); 

        chain *tmp = list;
        list = list->next;
        free( tmp );       
     }

    printf( "First cell: %d\n", list->contents );

    while ( list != NULL )
    {
        chain *tmp = list;
        list = list->next;
        free( tmp );
    }

    return 0;  
} 
list = malloc(sizeof(chain));     
p = list;     

for(index=0;index<10;index++) {       
    (*p).contents = index;       
    (*p).next = malloc(sizeof(chain));       
    p = (*p).next; 
   } ;  
while (index < 9) { 
    printf("cell # %d: %d\n",index,(*p).contents);       
    p = (*p).next;       
    free(pointer);       
    pointer = p;       
    index++; 
  } ; 
printf("First cell: %d\n",(*list).contents); 
#include <stdlib.h>
#include <stdio.h>

typedef struct chain
{
    struct chain *next;       
    int contents;  
} chain; 

int main( void )  
{
    const int N = 10;

    chain *list = NULL;

    chain **current = &list;

    for ( int i = 0; i < N; i++ ) 
    {
        *current = malloc( sizeof( chain ) );
        ( *current )->contents = i;       
        ( *current )->next = NULL;       
        current = &( *current )->next;
    } ;     

    for ( int i = 0; i < N - 1; i++ )
    {
        printf( "cell # %d: %d\n", i,  list->contents ); 

        chain *tmp = list;
        list = list->next;
        free( tmp );       
     }

    printf( "First cell: %d\n", list->contents );

    while ( list != NULL )
    {
        chain *tmp = list;
        list = list->next;
        free( tmp );
    }

    return 0;  
} 
cell # 0: 0
cell # 1: 1
cell # 2: 2
cell # 3: 3
cell # 4: 4
cell # 5: 5
cell # 6: 6
cell # 7: 7
cell # 8: 8
First cell: 9