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

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

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

环境: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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)