成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

C++語言學(xué)習(xí)(二十)——自定義內(nèi)存管理

C++語言學(xué)習(xí)(二十)——自定義內(nèi)存管理

一、統(tǒng)計(jì)類對(duì)象中成員變量的訪問次數(shù)

mutable是為了突破const函數(shù)的限制而設(shè)計(jì)的,mutable修飾的成員變量將永遠(yuǎn)處于可改變的狀態(tài)。mutable成員變量破壞了只讀對(duì)象的內(nèi)部狀態(tài),而const成員函數(shù)保證只讀對(duì)象的狀態(tài)不變性,因此mutable成員變量無法保證只讀對(duì)象狀態(tài)的不變性。

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信平臺(tái)小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了云南免費(fèi)建站歡迎大家使用!

#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函數(shù)內(nèi)部也可以改變其值,但mutable破壞了只讀對(duì)象狀態(tài)的不變性,所以不推薦。

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

上述代碼使用指針常量統(tǒng)計(jì)訪問成員變量的次數(shù),不會(huì)破壞只讀對(duì)象的狀態(tài)不變性。

二、new/delete操作符重載

1、new/delete操作符本質(zhì)

new/delete的本質(zhì)是C++預(yù)定義的操作符,C++語言規(guī)范對(duì)new/delete操作符做出了嚴(yán)格的規(guī)范:
A、new關(guān)鍵字用于獲取足夠的內(nèi)存空間(默認(rèn)為堆空間),在獲取的空間中調(diào)用構(gòu)造函數(shù)創(chuàng)建對(duì)象。
B、delete調(diào)用析構(gòu)函數(shù)銷毀對(duì)象,歸還對(duì)象所占用的空間(默認(rèn)為堆空間)。
C++語言中可以重載new/delete操作符,重載new/delete操作符的意義在于改變動(dòng)態(tài)對(duì)象創(chuàng)建時(shí)的內(nèi)存分配方式,可以將new創(chuàng)建的對(duì)象分配在??臻g、靜態(tài)存儲(chǔ)空間、指定地址空間。
new/delete操作符支持全局重載、局部重載,但不推薦對(duì)new/delete操作符進(jìn)行全局重載,通常對(duì)new/delete操作符進(jìn)行局部重載,如針對(duì)具體的類進(jìn)行new/delete操作符重載。
new/delete操作符重載函數(shù)默認(rèn)為靜態(tài)函數(shù),無論是否顯示聲明static關(guān)鍵字。

    //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、在靜態(tài)存儲(chǔ)區(qū)創(chuàng)建對(duì)象

#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會(huì)創(chuàng)建Test對(duì)象到靜態(tài)存儲(chǔ)空間中,從打印結(jié)果可以知道new創(chuàng)建Test對(duì)象時(shí)先調(diào)用new操作符重載函數(shù),在返回的空間中再調(diào)用Test構(gòu)造函數(shù)。

3、在指定地址創(chuàng)建對(duì)象

在類中對(duì)new/delete操作符進(jìn)行重載,在new操作符重載函數(shù)中返回指定的地址,在delete操作符重載函數(shù)中標(biāo)記對(duì)應(yīng)的地址可用。

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

上述代碼中,可以在指定地址空間創(chuàng)建對(duì)象,也可以不指定地址空間,此時(shí)在堆空間創(chuàng)建對(duì)象。

4、在棧空間創(chuàng)建對(duì)象

#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);
    //在??臻g創(chuàng)建對(duì)象
    Test* pTest = new(&test) Test(1000);

    return 0;
}
上述代碼中,可以使用new操作符的默認(rèn)實(shí)現(xiàn)在??臻g創(chuàng)建對(duì)象。

5、new[]/delete[]關(guān)鍵字

new[]/delete[]關(guān)鍵字與new/delete關(guān)鍵字完全不同,是一組全新的關(guān)鍵字。
new[]關(guān)鍵字用于創(chuàng)建動(dòng)態(tài)對(duì)象數(shù)組,delete[]關(guān)鍵字用于銷毀動(dòng)態(tài)對(duì)象數(shù)組。new[]/delete[]關(guān)鍵字可以進(jìn)行重載,用于優(yōu)化內(nèi)存管理方式。new[]關(guān)鍵字返回的空間大小通常大于預(yù)期的動(dòng)態(tài)數(shù)組空間大小。

#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[]關(guān)鍵字。

當(dāng)前題目:C++語言學(xué)習(xí)(二十)——自定義內(nèi)存管理
分享地址:http://jinyejixie.com/article30/iishpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管品牌網(wǎng)站建設(shè)、全網(wǎng)營銷推廣小程序開發(fā)、網(wǎng)站制作、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)
清水县| 金秀| 关岭| 铜山县| 东港市| 建始县| 张家界市| 黎城县| 普兰店市| 永春县| 鄱阳县| 双鸭山市| 若尔盖县| 南雄市| 灯塔市| 阿拉善左旗| 蒙城县| 金沙县| 公主岭市| 桑日县| 依兰县| 新龙县| 武城县| 拜城县| 江都市| 犍为县| 汉阴县| 牙克石市| 米脂县| 阿尔山市| 宁陵县| 临潭县| 通辽市| 齐齐哈尔市| 常宁市| 辉县市| 沂南县| 余姚市| 肃宁县| 达孜县| 晴隆县|