有关“CRT detect that the application wrote to memory after end of heap buffer”问题的解决
来源:互联网 发布:京东大数据平台架构 编辑:程序博客网 时间:2024/06/05 05:44
今天在编写函数求解矩阵的逆时出现“CRT detect that the application wrote to memory after end of heap buffer”的问题,主要代码如下:
a = (double**)malloc(N*sizeof(int*));
L = (double**)malloc(N*sizeof(int*));
U = (double**)malloc(N*sizeof(int*));
out = (double**)malloc(N*sizeof(int*));
out1 = (double**)malloc(N*sizeof(int*));
r = (double**)malloc(N*sizeof(int*));
u = (double**)malloc(N*sizeof(int*));
for(count=0;count<N;count++)
{ a[count]=(double*)malloc(N*sizeof(double));
L[count]=(double*)malloc(N*sizeof(double));
U[count]=(double*)malloc(N*sizeof(double)));
out[count]=(double*)malloc(N*sizeof(double));
out1[count]=(double*)malloc(N*sizeof(double));
r[count]=(double*)malloc(N*sizeof(double));
u[count]=(double*)malloc(N*sizeof(double));
}
memset( &a[0][0] , 1, N*N);
memset( &L[0][0] , 1 , N*N);
memset( &U[0][0] , 1 , N*N);
memset( &out[0][0] , 1 , N*N);
memset( &out1[0][0] , 1 , N*N);
memset( &r[0][0] , 1 , N*N);
memset( &u[0][0] , 1 , N*N);
... ...
for (count=0;count<N;count++)
{
free(a[count]);
free(L[count]);
free(U[count]);
free(out[count]);
free(out1[count]);
free(r[count]);
free(u[count]);
}
free(a);
free(L);
free(U);
free(out);
free(out1);
free(r);
free(u);
return 1;
}
调试时是在free处出现错误,经过分析查资料知道是memset函数处出现了问题,应该改为
memset( &a[0][0] , 1, sizeof(a));
memset( &L[0][0] , 1 , sizeof(L));
memset( &U[0][0] , 1 , sizeof(U));
memset( &out[0][0] , 1 , sizeof(out));
memset( &out1[0][0] , 1 , sizeof(out1));
memset( &r[0][0] , 1 , sizeof(r));
memset( &u[0][0] , 1 , sizeof(u));
或
for (count=0;count<N;count++)
{
memset(a[count], 1, N*sizeof(double));
memset(L[count], 1, N*sizeof(double));
memset(U[count], 1, N*sizeof(double));
memset(out[count], 1, N*sizeof(double));
memset(out1[count], 1, N*sizeof(double));
memset(r[count], 1, N*sizeof(double));
memset(u[count], 1, N*sizeof(double));
}
原因:出现越界,初始化数组时写入了未分配的内存,并且在sizeof(int *) <= sizeof(double)的时候是成立的,所以在写入长度小于分配长度时不报错,但这样不合理,应该初始化数组的所有元素,以防不可预知的错误。
- 有关“CRT detect that the application wrote to memory after end of heap buffer”问题的解决
- CRT detected that the application wrote to memory after end of heap buffer 问题(转)
- CRT detected that the application wrote to memory after after the end of heap buffer
- 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
- 编译错误----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
- 内存错误: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
- 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
- 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
- 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
- 【debug】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
- 释放内存free()错误:CRT detected that the application wrote to memory after end of heap buffer:内存访问违规
- Tomcat+MySQL的配置(windows下)
- python入门学习
- Mysql常用的几种SQL提示
- void f() const 常成员函数
- eclipse插件
- 有关“CRT detect that the application wrote to memory after end of heap buffer”问题的解决
- Sql Server 取出多个字段列中的最大值和最小值
- eclipse 导出可运行jar包 cmd命令执行java **.jar时找不到第三方jar包问题
- hdu 1081 最大子矩阵求和问题
- cocos2d-x 2.2.2 android win7平台环境配置
- 初识HTML
- Python中zip()函数用法举例
- NULL空记录引起的查询错误
- 三、集合数据类型Collection Data Types