C++語言學習(二十)——自訂記憶體管理

來源:互聯網
上載者:User

標籤:===   擷取   func   因此   []   turn   mutable   訪問   member   

C++語言學習(二十)——自訂記憶體管理一、統計類對象中成員變數的訪問次數

mutable是為了突破const函數的限制而設計的,mutable修飾的成員變數將永遠處於可改變的狀態。mutable成員變數破壞了唯讀對象的內部狀態,而const成員函數保證唯讀對象的狀態不變性,因此mutable成員變數無法保證唯讀對象狀態的不變性。

#include <iostream>using namespace std;class Test{public:    Test():m_count(0)    {    }    void setValue(const int value)    {        m_count++;        m_data = value;    }    int getValue()    {        m_count++;        return m_data;    }    int getValue()const    {        m_count++;        return m_data;    }    int getCount()const    {        return m_count;    }private:    int m_data;    mutable int m_count;};int main(int argc, char *argv[]){    Test test1;    test1.setValue(100);    cout << test1.getCount() << endl;    const Test test2;    test2.getValue();    cout << test2.getCount() << endl;    return 0;}

上述代碼使用mutable修飾成員變數m_count,確保在const函數內部也可以改變其值,但mutable破壞了唯讀對象狀態的不變性,所以不推薦。

#include <iostream>using namespace std;class Test{public:    Test():m_count(new int(0))    {    }    void setValue(const int value)    {        *m_count = *m_count + 1;        m_data = value;    }    int getValue()    {        *m_count = *m_count + 1;        return m_data;    }    int getValue()const    {        *m_count = *m_count + 1;        return m_data;    }    int getCount()const    {        return *m_count;    }private:    int m_data;    int*  const m_count;};int main(int argc, char *argv[]){    Test test1;    test1.setValue(100);    cout << test1.getCount() << endl;    const Test test2;    test2.getValue();    cout << test2.getCount() << endl;    return 0;}

上述代碼使用指標常量統計訪問成員變數的次數,不會破壞唯讀對象的狀態不變性。

二、new/delete操作符重載1、new/delete操作符本質

new/delete的本質是C++預定義的操作符,C++語言規範對new/delete操作符做出了嚴格的規範:
A、new關鍵字用於擷取足夠的記憶體空間(預設為堆空間),在擷取的空間中調用建構函式建立對象。
B、delete調用解構函式銷毀對象,歸還對象所佔用的空間(預設為堆空間)。
C++語言中可以重載new/delete操作符,重載new/delete操作符的意義在於改變動態對象建立時的記憶體配置方式,可以將new建立的對象分配在棧空間、靜態儲存空間、指定地址空間。
new/delete操作符支援全域重載、局部重載,但不推薦對new/delete操作符進行全域重載,通常對new/delete操作符進行局部重載,如針對具體的類進行new/delete操作符重載。
new/delete操作符重載函數預設為靜態函數,無論是否顯示聲明static關鍵字。

    //static member function    void* operator new(unsigned int size)    {        void* ret = NULL;        /* ret point to allocated memory */        return ret;    }    //static member function    void operator delete(void* p)    {        /* free the memory which is pointed by p */    }
2、在靜態儲存區建立對象
#include <iostream>using namespace std;class Test{private:    static const unsigned int COUNT = 4;    static char c_buffer[];    static char c_map[];    int m_value;public:    Test(int value = 0)    {        m_value = value;        cout << "value : " << value << endl;    }    //static member function    void* operator new (unsigned int size)    {        void* ret = NULL;        for(int i = 0; i < COUNT; i++)        {            if( !c_map[i] )            {                c_map[i] = 1;                ret = c_buffer + i * sizeof(Test);                cout << "succeed to allocate memory: " << ret << endl;                break;            }        }        return ret;    }    //static member function    void operator delete (void* p)    {        if( p != NULL )        {            char* mem = reinterpret_cast<char*>(p);            int index = (mem - c_buffer) / sizeof(Test);            int flag = (mem - c_buffer) % sizeof(Test);            if( (flag == 0) && (0 <= index) && (index < COUNT) )            {                c_map[index] = 0;                cout << "succeed to free memory: " << p << endl;            }        }    }    int getValue()const    {        return m_value;    }};char Test::c_buffer[sizeof(Test) * Test::COUNT] = {0};char Test::c_map[Test::COUNT] = {0};int main(int argc, char *argv[]){    cout << "===== Test Single Object =====" << endl;    Test* pt = new Test(1);    delete pt;    cout << "===== Test Object Array =====" << endl;    Test* pa[5] = {0};    for(int i=0; i<5; i++)    {        pa[i] = new Test(100 + i);        cout << "pa[" << i << "] = " << pa[i] << endl;    }    for(int i=0; i<5; i++)    {        cout << "delete " << pa[i] << endl;        if(pa[i] != NULL)        {            delete pa[i];        }    }    return 0;}

上述代碼,new會建立Test對象到靜態儲存空間中,從列印結果可以知道new建立Test對象時先調用new操作符重載函數,在返回的空間中再調用Test建構函式。

3、在指定地址建立對象

在類中對new/delete操作符進行重載,在new操作符重載函數中返回指定的地址,在delete操作符重載函數中標記對應的地址可用。

#include <iostream>#include <string>#include <cstdlib>using namespace std;class Test{    static unsigned int c_count;    static char* c_buffer;    static char* c_map;    int m_value;public:    static bool SetMemorySource(char* memory, unsigned int size)    {        bool ret = false;        c_count = size / sizeof(Test);        ret = (c_count && (c_map = reinterpret_cast<char*>(calloc(c_count, sizeof(char)))));        if( ret )        {            c_buffer = memory;        }        else        {            free(c_map);            c_map = NULL;            c_buffer = NULL;            c_count = 0;        }        return ret;    }    void* operator new (unsigned int size)    {        void* ret = NULL;        if( c_count > 0 )        {            for(int i=0; i<c_count; i++)            {                if( !c_map[i] )                {                    c_map[i] = 1;                    ret = c_buffer + i * sizeof(Test);                    cout << "succeed to allocate memory: " << ret << endl;                    break;                }            }        }        else        {            ret = malloc(size);        }        return ret;    }    void operator delete (void* p)    {        if( p != NULL )        {            if( c_count > 0 )            {                char* mem = reinterpret_cast<char*>(p);                int index = (mem - c_buffer) / sizeof(Test);                int flag = (mem - c_buffer) % sizeof(Test);                if( (flag == 0) && (0 <= index) && (index < c_count) )                {                    c_map[index] = 0;                    cout << "succeed to free memory: " << p << endl;                }            }            else            {                free(p);            }        }    }};unsigned int Test::c_count = 0;char* Test::c_buffer = NULL;char* Test::c_map = NULL;int main(int argc, char *argv[]){    char buffer[12] = {0};    Test::SetMemorySource(buffer, sizeof(buffer));    cout << "===== Test Single Object =====" << endl;    Test* pt = new Test;    delete pt;    cout << "===== Test Object Array =====" << endl;    Test* pa[5] = {0};    for(int i=0; i<5; i++)    {        pa[i] = new Test;        cout << "pa[" << i << "] = " << pa[i] << endl;    }    for(int i=0; i<5; i++)    {        cout << "delete " << pa[i] << endl;        delete pa[i];    }    return 0;}

上述代碼中,可以在指定地址空間建立對象,也可以不指定地址空間,此時在堆空間建立對象。

4、在棧空間建立對象
#include <iostream>using namespace std;class Test{    int m_value;public:    Test(int value = 0)    {        m_value = value;        cout << "value : " << m_value << endl;        cout << "this : " << this << endl;    }};int main(int argc, char *argv[]){    Test test(100);    //在棧空間建立對象    Test* pTest = new(&test) Test(1000);    return 0;}上述代碼中,可以使用new操作符的預設實現在棧空間建立對象。
5、new[]/delete[]關鍵字

new[]/delete[]關鍵字與new/delete關鍵字完全不同,是一組全新的關鍵字。
new[]關鍵字用於建立動態對象數組,delete[]關鍵字用於銷毀動態對象數組。new[]/delete[]關鍵字可以進行重載,用於最佳化記憶體管理方式。new[]關鍵字返回的空間大小通常大於預期的動態數組空間大小。

#include <iostream>#include <string>#include <cstdlib>using namespace std;class Test{    int m_value;public:    Test(int value = 0)    {        m_value = value;    }    ~Test()    {    }    void* operator new (unsigned int size)    {        cout << "operator new: " << size << endl;        return malloc(size);    }    void operator delete (void* p)    {        cout << "operator delete: " << p << endl;        free(p);    }    void* operator new[] (unsigned int size)    {        cout << "operator new[]: " << size << endl;        return malloc(size);    }    void operator delete[] (void* p)    {        cout << "operator delete[]: " << p << endl;        free(p);    }};int main(int argc, char *argv[]){    Test* pt = NULL;    pt = new Test;    delete pt;    pt = new Test[5];    delete[] pt;    return 0;}

上述代碼中,重載了new[]/delete[]關鍵字。

C++語言學習(二十)——自訂記憶體管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.