谁能帮小弟解释一下delete到底怎么了?

yun_5025 2005-05-07 03:49:48
问题是这样的:
小弟在学《数据结构题集》(严蔚敏 吴伟民)第一章的算法设计题1.17有关裴波那契序列时

原题如下:
已知k阶裴波那契序列的定义为
f(0)=f(1)=f(2)...=f(k-2)=0,f(k-1)=1;
f(n)=f(n-1)+f(n-2)+...+f(n-k),n=k,k+1,...
试编写求k阶裴波那契序列的第m项值的函数算法,k和m均以值调用的形式在函数参数表中出现。

问题本身没什么,我用VC++6.0新建了WIn32 Console Application工程,并将stdafx.h中的#include <stdio.h>替换为#include <iostream.h>然后写下了如下代码:
#include "stdafx.h"
long double FBNQK(int k,int m,long double pFBNQ[]);
int main(int argc, char* argv[])
{
int k,m;
long double V;
char YN;
do
{
cout<<"请输入裴波那契序列的阶数:\n";
cin>>k;
cout<<"请输入要获得的裴波那契序列的项号:\n";
cin>>m;
long double* pFBNQ=new long double[m];
if(pFBNQ == 0)
{
cout<<"内存分配失败!\n";
return 0;
}
V=FBNQK(k,m,pFBNQ);
if(V < 0)
{
cout<<"计算失败!\n";
}
else
{
cout<<"要获得的裴波那契序列的第"<<m<<"项的值为"<<V;
}
//出问题的地方
delete []pFBNQ;
cout<<"是否继续?<y继续>:";
cin>>YN;
}
while(YN == 'y');
return 0;
}
long double FBNQK(int k,int m,long double pFBNQ[])
{
int i;
if(k<2 || m<=0)
{
return -1;
}
if(m < k-1)
{
return 0;
}
else if(m==k-1 || m==k)
{
return 1;
}
else
{
for(i = 0; i < k-1; i++)
{
pFBNQ[i]=0;
}
pFBNQ[k-1]=1;
for(i = k; i <= m; i++)
{
pFBNQ[i] = pFBNQ[i-1] * 2 - pFBNQ[i-k];
}
return pFBNQ[m];
}
}

编译通过,但是执行时报错:
Debug Error!
Program: ...
DAMAGE:after Normal block(#44) at 0x00441A60

经过确认发现
delete []pFBNQ;
语句有问题,将该语句注释后一切正常,但是没有该语句不会造成内存泄漏吗?

我重新简化程序如下:
#include "stdafx.h"
int main(int argc, char* argv[])
{
int m;
cin>>m;
long double *pFBNQ=new long double[m];
delete []pFBNQ;
return 0;
}

这回一切正常。为什么,问题出在哪里?
希望指点,谢谢。
...全文
106 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yun_5025 2005-05-07
  • 打赏
  • 举报
回复
谢谢,我一时粗心在new时少分配了一个元素
jerry 2005-05-07
  • 打赏
  • 举报
回复
这个问题很可能是内存访问越界了. 仔细检查你有算法. 有没有对数组越界操作.

new 出来的 数组, 一定要用对应的 delete [] 来删除
yun_5025 2005-05-07
  • 打赏
  • 举报
回复
如果是delete [];编译都通不过
oyljerry 2005-05-07
  • 打赏
  • 举报
回复
数组的释放就是delete []
yun_5025 2005-05-07
  • 打赏
  • 举报
回复
delete []pFBNQ;
这行后面再加一行:
pFBNQ = NULL;//再试试

试了,还是那个错误,编译是没问题但是执行时就会出错:
编译通过,但是执行时报错:
Debug Error!
Program: ...
DAMAGE:after Normal block(#44) at 0x00441A60
theCFan 2005-05-07
  • 打赏
  • 举报
回复
在xp/vc6下面编译没有问题,
rockersz 2005-05-07
  • 打赏
  • 举报
回复
delete []pFBNQ;
这行后面再加一行:
pFBNQ = NULL;//再试试
代码请参考笔者的另一个资源。 我们用千万像素级相机拍摄的照片,通常达到好几兆,时间长了,相片多了,就很占空间,但是又舍不得删除。怎么办?减少图片的精度,也就减小了所占空间。事实上,用Windows XP自带的画图程序(Win7自带的画图程序没试过),打开图片,然后另存,即可发现图片体积大大减小,但是精度的损失用人眼是判断不出来的。可是如果每个图片都用画图程序打开再另存,势必很费时间。于是,小弟就开发了一个批量减少图片体积的小程序,可以递归执行。需要注意的是,要先选中递归复选框,然后再选择文件夹,否则不会递归。这也算是一个小bug吧。 We do pixel-level camera photographs, usually reaches several trillion, a long time, photos and more, it is the space, but they could not bear to delete. How to do? Reduce image precision, it reduces the footprint. In fact, with the Paint program that comes with Windows XP (Win7 Paint program that comes with not tried), open the image, then save it, you can find pictures volume greatly reduced, but the loss of accuracy of the human eye is not out of judgment. But if each picture are turned on and then save it with a drawing program, it is bound to be time-consuming. Thus, the younger brother developed a small batch volume reduction picture program can recursively. Note, first check the Recursive checkbox, and then select the folder, otherwise not recursively. It would be a little bug it.
(笔者原创,请勿下载后再上传以赚取积分) 我们用千万像素级相机拍摄的照片,通常达到好几兆,时间长了,相片多了,就很占空间,但是又舍不得删除。怎么办?减少图片的精度,也就减小了所占空间。事实上,用Windows XP自带的画图程序(Win7自带的画图程序没试过),打开图片,然后另存,即可发现图片体积大大减小,但是精度的损失用人眼是判断不出来的。可是如果每个图片都用画图程序打开再另存,势必很费时间。于是,小弟就开发了一个批量减少图片体积的小程序,可以递归执行。需要注意的是,要先选中递归复选框,然后再选择文件夹,否则不会递归。这也算是一个小bug吧。 We do pixel-level camera photographs, usually reaches several trillion, a long time, photos and more, it is the space, but they could not bear to delete. How to do? Reduce image precision, it reduces the footprint. In fact, with the Paint program that comes with Windows XP (Win7 Paint program that comes with not tried), open the image, then save it, you can find pictures volume greatly reduced, but the loss of accuracy of the human eye is not out of judgment. But if each picture are turned on and then save it with a drawing program, it is bound to be time-consuming. Thus, the younger brother developed a small batch volume reduction picture program can recursively. Note, first check the Recursive checkbox, and then select the folder, otherwise not recursively. It would be a little bug it.

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧