鱼C论坛

 找回密码
 立即注册
查看: 3089|回复: 2

C语言单链表的释放内存空间问题

[复制链接]
发表于 2018-11-17 11:22:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 萝卜luobo 于 2018-11-17 11:23 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Book
  4. {
  5.         char title[128];
  6.         char author[40];
  7.         struct Book *next;
  8. };

  9. void getInput(struct Book *book);
  10. void addBook(struct Book **library);
  11. void printLibrary(struct Book *library);
  12. void releaseLibrary(struct Book *library);

  13. void getInput(struct Book *book)
  14. {
  15.         printf("请输入书名:");
  16.         scanf("%s", book->title);
  17.         printf("请输入作者:");
  18.         scanf("%s", book->author);
  19. }

  20. void addBook(struct Book **library)
  21. {
  22.         struct Book *book, *temp;

  23.         book = (struct Book *)malloc(sizeof(struct Book));
  24.         if (book == NULL)
  25.         {
  26.                 printf("内存分配失败了!\n");
  27.                 exit(1);
  28.         }

  29.         getInput(book);

  30.         if (*library != NULL)
  31.         {
  32.                 temp = *library;
  33.                 *library = book;
  34.                 book->next = temp;       
  35.         }
  36.         else
  37.         {
  38.                 *library = book;
  39.                 book->next = NULL;
  40.         }
  41. }

  42. void printLibrary(struct Book *library)
  43. {
  44.         struct Book *book;
  45.         int count = 1;

  46.         book = library;
  47.         while (book != NULL)
  48.         {
  49.                 printf("Book%d: ", count);
  50.                 printf("书名: %s\n", book->title);
  51.                 printf("作者: %s\n", book->author);
  52.                 book = book->next;
  53.                 count++;
  54.         }
  55. }

  56. void releaseLibrary(struct Book *library)
  57. {
  58.        
  59.         while (library != NULL)
  60.         {
  61.                 free(library);
  62.                 library = library->next;
  63.         }
  64. }

  65. int main(void)
  66. {
  67.         struct Book *library = NULL;
  68.         int ch;

  69.         while (1)
  70.         {
  71.                 printf("请问是否需要录入书籍信息(Y/N):");
  72.                 do
  73.                 {
  74.                         ch = getchar();
  75.                 } while (ch != 'Y' && ch != 'N');

  76.                 if (ch == 'Y')
  77.                 {
  78.                         addBook(&library);
  79.                 }
  80.                 else
  81.                 {
  82.                         break;
  83.                 }
  84.         }

  85.         printf("请问是否需要打印图书信息(Y/N):");
  86.         do
  87.         {
  88.                 ch = getchar();
  89.         } while (ch != 'Y' && ch != 'N');

  90.         if (ch == 'Y')
  91.         {
  92.                 printLibrary(library);
  93.         }

  94.         releaseLibrary(library);

  95.         return 0;
  96. }
复制代码

这是小甲鱼老师的代码
为什么
  1. void releaseLibrary(struct Book *library)
  2. {
  3.        
  4.         while (library != NULL)
  5.         {
  6.                 library = library->next;
  7.                 free(library);
  8.         }
  9. }
复制代码

这样不可以,而是要这样
  1. void releaseLibrary(struct Book **library)
  2. {
  3.         struct Book *temp;
  4.         while(*library != NULL)
  5.         {
  6.                 temp = *library;
  7.                 *library =(*library)-> next;
  8.                 free(temp);
  9.         }
  10. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-11-17 15:46:54 | 显示全部楼层
第一张图里,执行完free(library)后library指向的空间的内容就不存在了,虽然library指针的值没变。但library的next找不到了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-17 16:20:17 | 显示全部楼层
用后面那短代码的时候需要,releaseLibrary(&library);  加个取地址;
第一段是,你都把library指向的内存空间释放掉啦,而在接下来的程序中你还在使用它,肯定会出错啊;
像这样先缓存下来就可以
  1. void releaseLibrary(struct Book *library)
  2. {
  3.         struct Book *temp;
  4.         while (library != NULL)
  5.         {
  6.                 temp = library->next;
  7.                 free(library);
  8.                 library = temp;
  9.         }
  10. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 03:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表