yokeyoke 2016-01-15 15:06 采纳率: 100%
浏览 3591
已采纳

【析构函数写法】如何使用析构函数释放类成员函数申请的堆内存

标题:【析构函数写法】如何使用析构函数释放类成员函数申请的堆内存

环境:win7 64位/AMD CPU/C++ GCC4.7.2/Codeblocks

详细描述:如下所示”代码块1“,每次在调用encrypt()函数时均会申请一次内存,如何在析构函数中一次性销毁所有产生的内存,”代码块2“的方法不符合要求

扩展:如果上述问题无较好答案,是否有其他方法可以避免在类函数中使用new,但也能达到目的
代码块1:

#include <iostream>
#include <string.h>
using namespace std;
class cipher
{
public:
    char key[128];
    cipher(const char *key_)
    {
        strcpy(key,key_);
    }



    char* encrypt(const char* text)
    {
        int a= (int((strlen(text)/128)+1))*128;
        //每运行一次则产生一个新内存
        char *tempResult=new char[a];
        strcpy(tempResult,text);
        strcat(tempResult,key);

        return tempResult;
    }


    ~cipher()
    {

    }

};

int main()
{
    const char* myKey="this is a key";
    cipher encryptBook(myKey);

    const char* engilishBookContent="this is the content of the English book.";
    cout<<encryptBook.encrypt(engilishBookContent)<<endl;

    const char* chineseBookContent="this is the content of the Chinese book.";
    cout<<encryptBook.encrypt(chineseBookContent)<<endl;
}

代码块2:

 class cipher
{
public:
    char key[128];
    char *tempResult;
    cipher(const char *key_)
    {
        strcpy(key,key_);
        tempResult = null;
    }

    char* encrypt(const char* text)
    {
        int a= (int((strlen(text)/128)+1))*128;
       if(tempResult != null){delete [] tempResult ;tempResult = null;}
       tempResult=new char[a];
        strcpy(tempResult,text);
        strcat(tempResult,key);

        return tempResult;
    }

    ~cipher()
    {
       if(tempResult != null){delete [] tempResult ;tempResult = null;}
    }

};
  • 写回答

8条回答 默认 最新

  • yokeyoke 2016-01-19 12:45
    关注
     #include <iostream>
    #include <string.h>
    using namespace std;
    class cipher
    {
    public:
        char key[128];
        cipher(const char *key_)
        {
            strcpy(key,key_);
            recordNew=NULL;//初始化指针数组
            qtyRecord=0;//初始化指针数组的元素个数
    
        }
    
    
        char* encrypt(const char* text)
        {
            int a= (int((strlen(text)/128)+1))*128;
            char *tempResult=new char[a];
            strcpy(tempResult,text);
            strcat(tempResult,key);
            record(tempResult);
            return tempResult;
        }
    
    
        void record(char *newChar)
        {
            cout<<"newChar=0X"<<hex<<(int)newChar<<endl;
            char **tempP=new char* [qtyRecord+1];
    
            //如果是第一次调用encrypt()函数,则直接赋值
            //如果不是则需要先拷贝原指针成员后,再添加新建的指针
            if (recordNew==NULL)
                tempP[0]=newChar;
            else
            {
                for (int i=0; i<=qtyRecord-1; ++i)
                {
                    tempP[i]=recordNew[i];
                    cout<<"i="<<i<<"  "<<"recordNew["<<i<<"]="<<hex<<(int)recordNew[i]<<endl;
                }
    
                tempP[qtyRecord]=newChar;
                delete[] recordNew;//删除老的recordNew指针数组
            }
    
            recordNew=new char*[qtyRecord+1];
    
            for(int j=0; j!=qtyRecord+1; ++j)
            {
                recordNew[j]=tempP[j];
                cout<<"recordNew["<<j<<"]="<<"0x"<<hex<<(int)recordNew[j]<<endl;
            }
    
            ++qtyRecord;//计数器加1
            delete[] tempP;//删除临时指针数组
            cout<<"****************************"<<endl;
        }
    
        ~cipher()
        {
            if (recordNew==NULL) return;
            delete[] recordNew;
        }
    //private:
        char** recordNew;
        int qtyRecord;
    
    
    };
    
    int main()
    {
        const char* myKey="this is a key";
        cipher encryptBook(myKey);
    
        const char* engilishBookContent="this is the content of the English book.";
        encryptBook.encrypt(engilishBookContent);
    //    cout<<encryptBook.encrypt(engilishBookContent)<<endl;
    
        const char* chineseBookContent="this is the content of the Chinese book.";
        encryptBook.encrypt(chineseBookContent);
    //    cout<<encryptBook.encrypt(chineseBookContent)<<endl;
    
        const char* mathBookContent="this is the content of the math book.";
        encryptBook.encrypt(mathBookContent);
    //    cout<<encryptBook.encrypt(mathBookContent)<<endl;
    
        const char* historyBookContent="this is the content of the history book.";
        encryptBook.encrypt(historyBookContent);
    //    cout<<encryptBook.encrypt(historyBookContent)<<endl;
    
        cout<<"++++++++++++++++++++++++++++"<<endl;
        cout<<encryptBook.recordNew[1]<<endl;
        encryptBook.~cipher();
        cout<<encryptBook.recordNew[1]<<endl;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题