书上代码:

#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. active Directory域服务安装配置

    1.在Windows功能启用 2.安装一直下一步即可, 添加用户 添加域管理员 将普通用户添加到Domain Admins里

  2. HPS—虚拟地址映射

    HPS 如何对FPGA外设进行操作?hardware:在Qsys中将外设连接到AXI bridge上software:映射外设物理地址到到应用程序可以操作的虚拟地址,应用程序通过得到的虚拟地址入口控制 ...

  3. AcWing 853. 有边数限制的最短路 bellman-ford 结构体

    //存在负权值 处理负环 //如果能求出来 一般是不存在负权回路 //如果有负回路 那最小距离可能是负无穷 #include <cstring> #include <iostream ...

  4. 4.万能的Map+模糊查询

    万能的Map 当数据或者属性很多的时候,可以选择性的单独改变密码或者用户名等等 UserMapper.java int updateUserByMap(Map<String,Object> ...

  5. logstash output到kafka记录与总结( No entry found for connection 2)

    简述 本文记录logstash的output配置为kafka的过程.这里是简单的例子,输入为stdin,本文主要目的是为了记录在这次配置过程中遇到的问题和解决的过程及总结. 关于kafka集群的搭建可 ...

  6. js实现上移下移

    直接上代码 //上移 var $up = $(".up") $up.click(function () { var $tr = $(this).parents("tr&q ...

  7. 攻防世界 robots题

    来自攻防世界 robots [原理] robots.txt是搜索引擎中访问网站的时候要查看的第一个文件.当一个搜索蜘蛛访问一个站点时,它会首先检查该站点根目录下是否存在robots.txt,如果存在, ...

  8. eclipse中使用maven update project功能后,默认又回到了jre 1.5的解决方案

    在maven项目中的pom.xml中添加以下节点,进行jre版本的配置,配置完后再进行项目更新后,并不会自动切换到jre1.5 添加在pom的url标签后面 <build>         ...

  9. leetcode929 Unique Email Addresses

    Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...

  10. AliWareMQ

    mq配置文件(Spring) 主要是顺序消息的配置,以及多实例的配置(需要在控制台配置p/c) <?xml version="1.0" encoding="UTF- ...