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

本文介绍了一个C++实现的Sequence类,用于存储递增的整数序列,并提供了展示序列的功能。探讨了内存管理和析构问题。

下面一道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能正确析构?

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值