user-0 提问: 有关单链表free失败的问题，在不加free程序是可以正常运行的

回复:
user: user-1
content: 你的malloc和free都不对应。你的malloc分配的单个的node节点，别指望通过free（list）来释放所有的节点。且你的那个list并没有通过malloc分配

user: user-2
content: free Deallocates or frees a memory block.  void free( void *memblock );  Function Required Header Compatibility  free <stdlib.h> and <malloc.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  None  Parameter  memblock  Previously allocated memory block to be freed  Remarks  The free function deallocates a memory block (memblock) that was previously allocated by a call to calloc, malloc, or realloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case of realloc). If memblock is NULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated by calloc, malloc, or realloc) may affect subsequent allocation requests and cause errors.  After a memory block has been freed, _heapmin minimizes the amount of free memory on the heap by coalescing the unused regions and releasing them back to the operating system. Freed memory that is not released to the operating system is restored to the free pool and is available for allocation again.  When the application is linked with a debug version of the C run-time libraries, free resolves to _free_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support.  Example  /* MALLOC.C: This program allocates memory with  * malloc, then frees the memory with free.  */  #include <stdlib.h>         /* For _MAX_PATH definition */ #include <stdio.h> #include <malloc.h>  void main( void ) {    char *string;     /* Allocate space for a path name */    string = malloc( _MAX_PATH );    if( string == NULL )       printf( "Insufficient memory available\n" );    else    {       printf( "Memory space allocated for path name\n" );       free( string );       printf( "Memory freed\n" );    } }   Output  Memory space allocated for path name Memory freed   Memory Allocation Routines  See Also   _alloca, calloc, malloc, realloc, _free_dbg, _heapmin

user: user-3
content: #include <stdio.h> #include <stdlib.h>  typedef struct Node {     int data;     struct Node *pNext; } Node, *list;  void Creat_list(list*l, int n); void shuchu(list l); void merge(list la, list lb, list *lc);  int  main(void) {     list la;     list lb;     list lc;      Creat_list(&la, 3);     shuchu(la);     Creat_list(&lb, 3);     shuchu(lb);      merge(la, lb, &lc);      shuchu(lc);      return 0; } void Creat_list(list *l, int n) {     int a;     int i;     list q;      *l = (list)malloc(sizeof(Node));     (*l)->pNext = NULL;     for (i = n; i > 0; i--)     {         q = (list)malloc(sizeof(Node));         scanf("%d", &a);         q->pNext = (*l)->pNext;         (*l)->pNext = q;         q->data = a;     } }  void shuchu(list l) {     list p = l->pNext;      while (p) {         printf("%d\t", p->data);         p = p->pNext;     }     putchar(10); }  void merge(list la, list lb, list *lc) {     list pa;     list pb;     list pc;      pa = la->pNext;     pb = lb->pNext;     *lc = pc = la;      while (pa && pb) {         if (pa->data <= pb->data) {             pc->pNext = pa;             pc = pa;             pa = pa->pNext;         } else {             pc->pNext = pb;             pc = pb;             pb = pb->pNext;         }     }     while (pa) {         pc->pNext = pa;         pc = pa;         pa = pa->pNext;     }     while (pb) {         pc->pNext = pb;         pc = pb;         pb = pb->pNext;     }     free(lb); }  合并算法有点问题，现已改好了，你可以跑一下试试； 1. 合并排序的参数，lb不需要传递二重指针，因为不要将lb设置为传入传出参数，只需要作为传入参数即可，并在merge函数里将其释放。或者在main函数里释放，都是可以的。 2. 合并排序时，有可能两个链表的长度不一样，那么就需要将链表较长的最后加到链表lc中。