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(# ...
随机推荐
- JavaScript encodeURI() 函数
encodeURI() 函数可把字符串作为 URI 进行编码. -------------------------------------------------------------------- ...
- android studio 新建项目 界面一直停在 【“building ‘ 项目名’ gradle project info”】
zhezhelin android studio 新建项目 界面一直停在 [“building ‘ 项目名’ gradle project info”] 安装了android studio 之后,按照 ...
- 【HDOJ】2289 Cup
二分.另外,圆台体积为v = PI*(r*r+r*R+R*R)*H/3.注意精度. #include <cstdio> #include <cmath> #define exp ...
- matlab制造一个64*64的仿真数据
fid = fopen('test_001.img','w'); r=random('Normal',100,0,64,64); z=random('Uniform',0,5,64,64); %%%% ...
- weblogic重置密码
1.备份DefaultAuthenticatorInit.ldift文件 cd /app/weblogic_cs/Oracle/Middleware/user_projects/domains/ntf ...
- Light OJ 1004 - Monkey Banana Problem(DP)
题目大意: 给你一菱形的数字阵,问从最上面走到最下面所能获得的最大值是多少? #include<cstdio> #include<cstring> #include<io ...
- 树(最小乘积生成树,克鲁斯卡尔算法):BOI timeismoney
The NetLine company wants to offer broadband internet to N towns. For this, it suffices to construct ...
- Hierarchy Viewer显示视图性能指标
Hierarchy Viewer默认打开“Tree View”窗口无法显示显示Performance indicators: 但选中根视图再点击按钮“Obtain layout times for t ...
- Single Number III——LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- MySQL 创建库
CREATE DATABASE IF NOT EXISTS database_name DEFAULT CHARSET utf8 COLLATE utf8_general_ci; 这种创建方式能保证数 ...