关于Debug Assertion Failed问题
书上代码:
#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",¤t->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问题的更多相关文章
- Qt 调试时的错误——Debug Assertion Failed!
在VS2008中写qt程序时调试出现此问题,但在release模式下就不存在,在网上搜罗了一圈,是指针的问题. 问题是这样的: 需要打开两个文件,文件中数据类型是float,我使用QVector进行保 ...
- 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 ...
- Debug Assertion Failed!
问题并没有解决..... 不知道怎么回事,先都没有这样的情况... VC++调程序出现如下错误: Debug Assertion Failed! Program: D:wyuS ...
- (转)Debug Assertion Failed! Expression: _pFirstBlock == pHead
最近在VS上开发C++程序时遇到了这个错误: Debug Assertion Failed! Expression:_pFirstBlock == pHead 如图: 点击Abort之后,查看调用 ...
- C++ error:Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlock)
Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlockUse) 关于上面这个错误,我在上一篇文章中的程序遇到过了 ...
- C++析构函数造成Debug Assertion Failed的问题
昨天写了两个程序,均出现了析构函数造成Debug Assertion Failed的问题,由于是初学c++怎么想也想不通问题出在哪里.今天早上经人指点终于明白问题所在了.下面贴出代码和问题解析:(以下 ...
- Debug Assertion Failed! Expression: _pFirstBlock == pHead
点击Abort之后,查看调用栈,发现异常在函数return时被时产生,进一步看是vector的析构函数被调用时产生,以前没开发过C++项目,没什么经验,这个错误让我很困惑,第一,我电脑上并没有f盘:第 ...
- opencv检错:程序运行过程正常,当跳出函数时出现断言错误(Debug Assertion Failed)
转载http://blog.csdn.net/u012327581/article/details/51351780 1.问题描述 在VS2015下配置好Opencv后,程序在函数运行过程中正常,调试 ...
- MFC:“Debug Assertion Failed!” ——自动生成的单文档程序项目编译运行就有错误
今天照着孙鑫老师的VC++教程学习文件的操作,VS2010,单文档应用程序,项目文件命名为File,也就有了自动生成的CFileDoc.CFileView等类,一进去就编译运行(就是最初自动生成的项目 ...
- 读书笔记——Windows核心编程(2)禁止C运行时触发的所有Debug Assertion Failed对话框
1 定义一个函数 void _invalid_parameter( const wchar_t * expression, const wchar_t * function, const wchar_ ...
随机推荐
- sublime 神一样的插件
专属配置 // 主题 "theme": "Boxy Tomorrow.sublime-theme", "theme_grid_border_size_ ...
- js变量提升的坑
关于js变量提升 变量提升 在js函数内部是可以直接修改全局的变量的,个人感觉是不好的设计, 但是确实存在这个概念 原理: 先查看有没有函数变量bb 查看形参有没有bb 查看全局有没有bb 报错, 找 ...
- 修改vsftpd的默认根目录/var/ftp/pub到其他目录
修改ftp的根目录只要修改/etc/vsftpd/vsftpd.conf文件即可: 加入如下几行: local_root=/var/www/html chroot_local_user=YES ano ...
- EF Code Frist 配置
通过Nuget安装Entity Framework, 创建model;[Table("")] 代表在数据库对应的名称,可以自定义规则,[Key]代表是主键 [Table(" ...
- APP项目下载及运行
1.首先下载Git 2.再下载安装node.js 3.dos窗口下载node.js依赖jar包 执行命令:npm install 4.从Git上down项目 5.运行项目 在项目根目录下 右键 打开 ...
- 【原】shell增加定时任务
通过shell脚本增加定时任务:#CRON_FILE="/var/spool/cron/root" (centos系统) CRON_FILE="/var/spool/cr ...
- 无显示器安装raspberry zero w树莓派 zero w
笔者的环境 1. Macbook 电脑用于烧录树莓派系统到SD卡 2. 树莓派 zero w 把SD卡放进读卡器,插到Mac电脑上, 打开命令行工具Terminal diskutil list列出di ...
- C#中的switch
switch 表达式支持的类型为 sbyte.byte.short.ushort.int.uint.long.ulong.char.string 或枚举类型,或者可以隐试转换到这些类型的自定义对象或其 ...
- nginx配置 yii2 URL重写规则 SSI配置使shtml
location / { // 加上红色部分 重写url try_files $uri $uri/ /index.php?$args; if (!-e $request_filename){ rewr ...
- h5 调起app 如果没安装就跳转下载
<!doctype html> <html> <head> <title></title> <meta charset="u ...