我的程序运行结果正确,但是在关闭时出现如下异常
Debug Assertion Failed!
Program: c:\temp\app.exe
File: dbgheap.c
Line: 1011
Expression: _CrtIsValidHeapPointer(pUserData)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts
(Press Retry to debug the application)
Abort Retry Ignore
经过设置断点,调试查出:我的一段程序中用malloc 分配了一段动态数组,但是达到free(matrix)时异常便发生请问我该如何解决这个问题,希望能够得到大家的帮助谢谢了
Debug Assertion Failed!
Program: c:\temp\app.exe
File: dbgheap.c
Line: 1011
Expression: _CrtIsValidHeapPointer(pUserData)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts
(Press Retry to debug the application)
Abort Retry Ignore
经过设置断点,调试查出:我的一段程序中用malloc 分配了一段动态数组,但是达到free(matrix)时异常便发生请问我该如何解决这个问题,希望能够得到大家的帮助谢谢了
for(i=0;i<=middle_l-1;i++)
for(j=0;j<=(kmax+2);j++)
matrix[i*(kmax+3)+j]=0;
if((fp1=fopen("input.dat","r"))==NULL)
{
printf("cannot open input.dat file\n");
}
fscanf(fp1,"%d,",&event_l);
fscanf(fp1,"%d,",&middle_l);
fscanf(fp1,"%d/",&kmax);
fprintf(fp1,"\n");
for(i=0;i<=middle_l-1;i++)
{
for(j=0;j<=kmax+2;j++)
{
if(j<=kmax+1)
fscanf(fp1,"%d,",&matrix[i*(kmax+3)+j]);
else if(j==kmax+2)
fscanf(fp1,"%d/",&matrix[i*(kmax+3)+j]);
}
fprintf(fp1,"\n");
}
fclose(fp1);
中件用到matrix 的地方都是函数调用,函数最后用free(matrix);释放
这样你的middle_l和kmax就和原来不一样了。这样肯定有可能会造成比原来的middle_l
和kmax要大。而一大动态数组就越界了。这就是你不能free的缘故。
NEW和Delete呢?