为什么本例中c++析构函数不能正确释放内存及析构

doing820000 2008-07-23 08:40:13
下面一道C++习题的疑问,希望大家能帮我解决

习题:
编写一个类Sequence,在自由存储区中按照升序存储整数值的递增序列,序列的长度和起始值
在构造函数中提供.确保该序列至少有两个值,默认10个值,从0开始(0,1,2,3,4,5,6,7,8,9).需要
有足够的内存空间来存储该序列,再用要求的内容来填充.
提供show()函数列出该序列,以确保正确创建Sequence对象.
确保在销毁Sequence对象时,释放分配给序列的内存(注意:确保释放所有的内存!).创建一个
默认序列和一个随机序列,来演示这个类的操作.


这是一个控制台应用程序,我创建了两个类和一个main()函数:
Integer:存储整数值的节点类
Sequence:序列类

代码如下:
Integer类
Integer头文件
//Integer.h

#ifndef INTEGER_H
#define INTEGER_H
#include <stdlib.h>
#include <iostream>

class Integer //class Integer
{
public:
Integer(int newInt=0,Integer* pNewNext=0); //元素构造函数
void setInt(int value,int i); //设置元素整数值
int getInt() const; //获取元素整数值
void setNext(Integer* pInt); //设置元素整数值
Integer* getNext() const; //获取下一个元素

private:
int Int; //元素整数值
Integer* pNext; //指向下一个元素
};

inline int random(int count) //随机函数
{
return 1+static_cast<int>(count*rand()/(RAND_MAX+1));
}

#endif


Integer源文件
//Integer.cpp

#include "Integer.h"
#include <iostream>

using std::cout;

//构造节点
Integer::Integer(int newInt,Integer* pNewNext)
{
Int=newInt;
pNext=pNewNext;
}

//设置整数值
void Integer::setInt(int value,int i)
{
Int=value;
Int+=i;
}

//获取整数值
int Integer::getInt() const
{
return Int;
}

//添加新元素到尾部
void Integer::setNext(Integer* pInt)
{
pNext=pInt;
}

//获取下一个元素
Integer* Integer::getNext() const
{
return pNext;
}





Sequence类
Sequence头文件
//Sequence.h

#ifndef SEQUENCE_H
#define SEQUENCE_H

#include "Integer.h"

class Sequence
{
public:
Sequence(int count=10,int startValue=0); //构造函数
Integer* getFirstInt(); //获取序列第一个元素指针
Integer* getNextInt(); //获取序列下一个元素指针
~Sequence(); //析构函数

void addInt(int value,int i); //添加序列元素
void show(); //显示序列

private:
int startValue; //序列起始值
int count; //序列元素个数
Integer* pHead; //序列头指针
Integer* pCurrent; //序列当前指针
Integer* pTail; //序列尾指针
};

#endif

Sequence源文件
//Sequence.cpp

#include <iostream>
#include "Sequence.h"
#include "Integer.h"

using std::cout;
using std::endl;


//构造序列
Sequence::Sequence(int count,int startValue)
{
pHead=pCurrent=pTail=0;
if(count<2)
count=2; //个数小于是2,就设置2
for(int i=0;i<count;i++)
addInt(startValue,i); //设置序列及其整数值
this->count=count;
this->startValue=startValue;
}

//析构
Sequence::~Sequence()
{

cout<<"called"<<endl;//跟踪
//删除节点
while(pCurrent=pHead->getNext())//当前指针指向下一个元素
{
cout<<"call"<<endl;//跟踪
delete pHead;//删除节点
pHead=pCurrent;//头指针指向下一个节点
}
delete pHead;//删除最后节点
}

//添加节点
void Sequence::addInt(int value,int i)
{
Integer* pInt=new Integer;//创建节点

if(pHead)//检查序列不为空
{
pInt->setInt(value,i);//设置整数值
pTail->setNext(pInt);//添加节点到尾部
}
else//序列为空
{
pInt->setInt(value,i);
pHead=pInt;//设置头节点
}
pTail=pInt;//尾指针指向尾部
}

//获取序列第一个元素指针
Integer* Sequence::getFirstInt()
{
pCurrent=pHead;
return pCurrent;
}

//获取序列下一个元素指针
Integer* Sequence::getNextInt()
{
if(pCurrent)
pCurrent=pCurrent->getNext();//当前指针不为空,指向下一个节点
else
{
pCurrent=pHead; //当前指针为空, 指向头节点
}

return pCurrent;
}

//显示序列
void Sequence::show()
{
int line=0;//每行个数

pCurrent=pHead;
do{
cout<<pCurrent->getInt()<<' ';//输出整数值
line++;
if(line%10==0)
cout<<endl; //个数达到10换行
pCurrent=pCurrent->getNext(); //指向下一个元素
}while(pCurrent!=0);

cout<<endl;//换行
}


main()函数

//main.cpp

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include "Integer.h"
#include "Sequence.h"

using std::cout;
using std::endl;

main()
{
Sequence list1;//默认序列
list1.show();//显示list1

Sequence list2(5,4);//赋值序列
list2.show();//显示list2

const int dimlimit=20;
srand((unsigned)time(0));//随机数种子

Sequence list3;
list3=Sequence(random(dimlimit),random(dimlimit));//随机序列
list3.show();//显示随机序列

return 0;
}

我的问题是为什么main()函数中定义的随机序列lists,也就是上面的这段代码:
Sequence list3;
list3=Sequence(random(dimlimit),random(dimlimit));//随机序列
list3.show();//显示随机序列
调用了show()函数后,不能正确释放内存及析构,而前面的list1,list2能正确析构?



...全文
371 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjb_yd 2008-07-23
  • 打赏
  • 举报
回复
浅拷贝不能满足要求
自己定义赋值函数,防止多个指针指向同一块内存,导致对同一块内存释放多次
kakashi0309 2008-07-23
  • 打赏
  • 举报
回复
构造函数使用的不对吧...
xkyx_cn 2008-07-23
  • 打赏
  • 举报
回复
如果要按:
Sequence list3;
list3=Sequence(random(dimlimit),random(dimlimit));//随机序列
list3.show();//显示随机序列
的方式调用,需要自己提供 拷贝构造函数和拷贝赋值函数
JPEXE 2008-07-23
  • 打赏
  • 举报
回复
Sequence list3;
list3=Sequence(random(dimlimit),random(dimlimit));//随机序列
list3.show();//显示随机序列


改成


Sequence list3(random(dimlimit),random(dimlimit));//随机序列
list3.show();//显示随机序列

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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