程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c++-【析構函數寫法】如何使用析構函數釋放類成員函數申請的堆內存

c++-【析構函數寫法】如何使用析構函數釋放類成員函數申請的堆內存

編輯:編程綜合問答
【析構函數寫法】如何使用析構函數釋放類成員函數申請的堆內存

標題:【析構函數寫法】如何使用析構函數釋放類成員函數申請的堆內存

環境: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;}
    }

};

最佳回答:


 #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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved