书上代码:

#include<stdio.h>
#include<stdlib.h> /* 提供malloc()、free()函数 */
#include<string.h> #define TSIZE 45 struct film {
char title [TSIZE];
int rating;
struct film *next; /* 指向链表的下一个结构 */
}; int main()
{
struct film *head=NULL; /* 链表头 */
struct film *prev,*current; /* 定义两个film结构型指针 */
char input[TSIZE]; /* 字符数组 */ puts("Enter first movie title:");
while(gets(input)!=NULL && input[0]!='\0') /* 接收键盘上输入的字符串 */
{
current=(struct film *)malloc(sizeof(struct film)); /* 为结构型分配足够的存储空间 */
if(head==NULL) /* 第一个节点为空 */
head=current; /* 则将地址赋值给头 */
else /* 后续节点 */
prev->next=current; /* 将地址赋给结构指针指向前一个结构中的next成员 */
current->next=NULL; /* 用空指针给current指向的第二个结构成员next赋空值 */
strcpy(current->title,input);/* 将电影名赋值给结构型中的标题 */
puts("Enter your rating <0-10>:");
scanf("%d",&current->rating);
while(getchar()!='\n')/* 接收回车符 */
continue;
puts("Enter next movie title (empty line to stop):");
prev=current;/* 接受下轮输入 */
} if(head==NULL)
printf("No data entered.");
else
printf("Here is the movie list:\n");
current=head;/* 指向头节点 */
while(current!=NULL)
{
printf("Movie: %s Rating: %d\n",current->title,current->rating);
current=current->next;/* 不断指向下一个 */
} current=head;/* 问题出在这里 */
while(current!=NULL)/* 内存使用完后释放内存 */
{
free(current);
current=current->next;
} printf("Bye!\n");
return 0;
}

  迷惑:

去掉 current=head; 后程序才能正常运作。
网上查了一下,也有人提过这个问题,但他的解决方法是将free直接去掉,运行就没问题了。
然后我就奇怪了,难道不用释放内存吗?

  解决:

//再来说说这个问题,释放的代码确实是错误的,正确的释放操作为:

/* 再定义一个结构体类型的指针 temp */
while(current)
{
temp = current;
current = current -> next;
free(temp);
}

  

关于Debug Assertion Failed问题的更多相关文章

  1. Qt 调试时的错误——Debug Assertion Failed!

    在VS2008中写qt程序时调试出现此问题,但在release模式下就不存在,在网上搜罗了一圈,是指针的问题. 问题是这样的: 需要打开两个文件,文件中数据类型是float,我使用QVector进行保 ...

  2. Solve Error Debug Assertion Failed Expression vector iterators incompatible Using PCL in Release Mode of VS2010

    When using PCL 1.4.0 in the release mode building under VS2010, we might sometime get the error &quo ...

  3. Debug Assertion Failed!

    问题并没有解决..... 不知道怎么回事,先都没有这样的情况... VC++调程序出现如下错误: Debug   Assertion   Failed!       Program:   D:wyuS ...

  4. (转)Debug Assertion Failed! Expression: _pFirstBlock == pHead

      最近在VS上开发C++程序时遇到了这个错误: Debug Assertion Failed! Expression:_pFirstBlock == pHead 如图: 点击Abort之后,查看调用 ...

  5. C++ error:Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlock)

    Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlockUse) 关于上面这个错误,我在上一篇文章中的程序遇到过了 ...

  6. C++析构函数造成Debug Assertion Failed的问题

    昨天写了两个程序,均出现了析构函数造成Debug Assertion Failed的问题,由于是初学c++怎么想也想不通问题出在哪里.今天早上经人指点终于明白问题所在了.下面贴出代码和问题解析:(以下 ...

  7. Debug Assertion Failed! Expression: _pFirstBlock == pHead

    点击Abort之后,查看调用栈,发现异常在函数return时被时产生,进一步看是vector的析构函数被调用时产生,以前没开发过C++项目,没什么经验,这个错误让我很困惑,第一,我电脑上并没有f盘:第 ...

  8. opencv检错:程序运行过程正常,当跳出函数时出现断言错误(Debug Assertion Failed)

    转载http://blog.csdn.net/u012327581/article/details/51351780 1.问题描述 在VS2015下配置好Opencv后,程序在函数运行过程中正常,调试 ...

  9. MFC:“Debug Assertion Failed!” ——自动生成的单文档程序项目编译运行就有错误

    今天照着孙鑫老师的VC++教程学习文件的操作,VS2010,单文档应用程序,项目文件命名为File,也就有了自动生成的CFileDoc.CFileView等类,一进去就编译运行(就是最初自动生成的项目 ...

  10. 读书笔记——Windows核心编程(2)禁止C运行时触发的所有Debug Assertion Failed对话框

    1 定义一个函数 void _invalid_parameter( const wchar_t * expression, const wchar_t * function, const wchar_ ...

随机推荐

  1. C++雾中风景番外篇4:GCC升级二三事

    最近将手头上负责的项目代码从GCC 4.8.2升级到了GCC 8.2.(终于可以使用C++17了,想想后续的开发也是很美好啊~~)不过这个过程之中也遇到了一些稀奇古怪的问题,在这里做一个简单的记录,希 ...

  2. Python requests库模拟浏览器行为的一些技巧记录

    如下都是一些经验之谈,不定期更新,喜欢可以关注哦. 忽略ssl报错 一些证书问题会导致程序报错,解决方法为在发送请求的时候,带上verify=False参数即可: result = requests. ...

  3. opencv:霍夫圆检测

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  4. bugku 细心

    打开链接会看到提醒404 显示不能访问 然后用御剑 扫描一下 然后会发现另一个 网址 然后打开 发现 有一个/result.php然后改一下 网址会发现 另一个网页 然后利用提示 将链接的后缀名改成? ...

  5. Python 树莓派 引脚

    #!/usr/bin/python3 import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OU ...

  6. SpringAOP学习之5种通知

    一.Spring的AOP分为以下5种类型通知 ①前置通知(Before):在连接点执行前执行该通知 ②正常返回通知(AfterReturning):在连接点正常执行完后执行该通知,若目标方法执行异常则 ...

  7. OracleDBConsoleorcl 服务无法启动:Agent process exited abnormally during initialization.

    OracleDBConsoleorcl 服务无法启动 在事件查看器里看到 Agent process exited abnormally during initialization.的记录.知道是因为 ...

  8. CI 框架多表关联查询

    public function getCollectData($limit, $page, $search_user, $search_phone, $orderfield, $ordertype) ...

  9. 喵星之旅-狂奔的兔子-docker安装和基本使用

      一.前提条件 目前,CentOS 仅发行版本中的内核支持 Docker. 位.系统内核版本为 3.10 以上. 位系统.参考喵星之旅-狂奔的兔子-linux安装 二.CentOS 7下安装 Doc ...

  10. Linux-VMware 15 虚拟机黑屏问题

    VMware 15 虚拟机黑屏问题   最近终于舍弃win7,换了win10的操作系统...   VM12不兼容,各种问题频出,于是换了VM15. 新装了kali2019.03,结果刚装好不久,在某一 ...