CRT detected that the application wrote to memory after end of heap buffer.
很多人的解释都不一样, 我碰到的问题是,开辟的内存空间小于操作的内存空间.也就是说,我free的内存越界了.
这是我开辟链表结构体内存的代码:
PNODE Create() {
int len; //total count of nodes to be created.
int i;
int val; //temp value for the current node.
printf("enter the size of nodes:");
scanf("%d", &len);
PNODE pHead = (PNODE)malloc(sizeof(PNODE));
pHead->pNext = NULL;
PNODE pTail = pHead;
if(NULL == pHead) {
printf("allocate memory failed!");
exit();
}
for (i = ; i < len; i++)
{
PNODE pCur = (PNODE)malloc(sizeof(PNODE));
if(NULL == pCur) {
printf("allocate memory failed!");
exit();
}
printf("enter the %d-th value : ", i + );
scanf("%d", &val);
pCur->data = val;
//set the new node as the tail node.
pTail->pNext = pCur;
pCur->pNext = NULL;
pTail = pCur;
}
return pHead;
}
我是这样定义结构体的:
typedef struct node {
int data;
struct node * pNext;
} NODE, * PNODE;
删除元素时(报错的代码)为:
bool Delete(PNODE pHead, int pos, int *v) {
int i = -;
PNODE pNode = pHead;
while((i < pos - ) && pNode != NULL) {
pNode = pNode->pNext;
i++;
}
if(pos < i || pNode == NULL)
return false;
PNODE pTmp = pNode->pNext; //store to free later.
*v = pTmp->data;
pNode->pNext = pNode->pNext->pNext;
free(pTmp);
pTmp = NULL;
return true;
}
这段代码我翻来覆去地调试,发现所有的节点的指针域和数据域都是我期待的.就是在free的时候报了错.
原来是我开辟内存的时候,大小指定错了,应该把:
PNODE pNew = (PNODE)malloc(sizeof(PNODE));
改为:
PNODE pNew = (PNODE)malloc(sizeof(NODE));
开辟节点的大小应该为结构体的大小,其实我在'插入'新节点的时候,这行代码就写错了,但是不会报错.我觉得应该是没有释放.
CRT detected that the application wrote to memory after end of heap buffer.的更多相关文章
- 内存错误:CRT detected that the application wrote to memory after end of heap buffer
今天调试测试代码时,发现在用完了new出来的内存buf后,在执行delete时报错了,具体信息为: HEAP_CORRUPTION_DETECTED: after Normal block(#908) ...
- C语言错误: CRT detected that the application wrote to memory after end of heap buffer
CRT detected that the application wrote to memory after end of heap buffer 多是中间对其进行了一些操作,在程序结束处,释放内存 ...
- [vs执行报错] CRT detected that the application wrote to memory after end of heap buffer
CRT 是c/c++ run-time lib , 是程序执行时所需的核心库. 这个错误是由于以对内在操作的过程中.所写的地址超出了.所分配内在的边界 有个建议是: 1.内存申请多少释放多少,释放掉你 ...
- JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xfe
JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xfe 在使用Jni ...
- Fatal Error -26000: Not enough memory (12320 bytes) for “new buffer in LrwSrvNetTaskIt 问题解决及lr脚本心得
Fatal Error -26000: Not enough memory (12320 bytes) for “new buffer in LrwSrvNetTaskIt 问题解决及lr脚本心得 2 ...
- C++ 编译器内存错误 after Normal block。。。
解决 after Normal block(#908) at 0x399EC0. CRT detected that the application wrote to memory after end ...
- windows c++ 错误汇总
1.fatal error C1900 错误:fatal error C1900: “P1”(第“20081201”版)和“P2”(第“20080116”版)之间 Il 不匹配 检查之后发现jepgl ...
- heap corruption detected错误解决方法调试方法以及内存管理相关
1.heap corruption detected http://vopit.blog.51cto.com/2400931/645980 heap corruption detected:aft ...
- 内存溢出(heap corruption detected:)
今天又遇到了上次出现的bug,然后百度了一下,想起来这是内存溢出的毛病,故记录下来! 出现的问题就是这样: heap corruption detected: after normal block(# ...
随机推荐
- Find The Multiple(poj 1426)
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- VC版本的MakeObjectInstance把WNDPROC映射到类的成员函数
这段时间用VC封装Windows类库,没有MakeObjectInstance处理窗口消息确实不爽,又不想使用MFC的消息映射,这玩意的效率和美观只能呵呵. 至于MakeObjectInstance是 ...
- Delphi判断文件是否正在被使用(CreateFile也可以只是为了读取数据,而不是创建)
首先,我们先来认识下CreateFile函数,它的原型如下 HANDLE CreateFile( LPCTSTR lpFileName, //指向文件名的指针 DWORD dwDesired ...
- build Intent
Intent用于activity之间, fragmetn之间, 或者APP间通信, 主要包含数据和Action两部分: 常见的action是字符串形式的activity指定 Intent intent ...
- 网络流(最大流):COGS 28 [NOI2006] 最大获利
28. [NOI2006] 最大获利 ★★★☆ 输入文件:profit.in 输出文件:profit.out 简单对比 时间限制:2 s 内存限制:512 MB [问题描述] 新的技术 ...
- The game of life(生命游戏)新算法
我写了一种常见的实现算法,和另一种新算法,即不是每次循环计算每个细胞的周围细胞数来产生下一时刻,而是每次每个产生状态变化的细胞主动通知周围的邻居,因此每个细胞增加一个用来记录邻居数的字段.由邻居数决定 ...
- N - Is It A Tree?(判断环)
题意,就是判断这点点是不是组成的一颗树,也就是判断是否有环,就是没看出来如果是森林怎么办,试一试吧,最可恶的还没有说有多少节点.....就是个坑 /////////////////////////// ...
- C - Catch That Cow
题目大意 农民约翰需要抓住他的牛,他和他的牛在一条直线上(估计是一维生物),约翰在NN; ; ; i<; i++) { ) q- ...
- Linux下设置允许myql数据库远程连接
系统环境: Linux ubuntu --generic #-Ubuntu SMP Thu Apr :: UTC x86_64 x86_64 x86_64 GNU/Linux 系统中已经安装好mysq ...
- PHP学习之[第08讲]数据库MySQL基础之增删改查
一.工具: 1.phpMyAdmin (http://www.phpmyadmin.net/) 2.Navicat (http://www.navicat.com/) 3.MySQL GUI Tool ...