重载new / delete 操作符

静态存储区创建动态对象

#include <iostream>
#include <string>

using namespace std;

class Test
{
    static const  int count = 4;
    static char m_buffer[];
    static char m_map[];

    int m_value;
public:
    void* operator new (unsigned int size)
    {
        void* ret = NULL;
        for(int i = 0; i < count; i++)
        {
            if(!m_map[i])
            {
                m_map[i] =1;
                ret = m_buffer + i * sizeof(Test);
                cout <<"succeed to allocate memory:" << ret << endl;
                break;
            }
        }
        return ret;
    }

    void operator delete(void* p)
    {
        if(p != NULL)
        {
            char* mem = reinterpret_cast<char*>(p);
            int index = (mem - m_buffer) / sizeof(Test);
            int flag = (mem -m_buffer) % sizeof (Test);

            if( (flag == 0) && (0 <= index) && (index < count))
            {
                m_map[index] = 0;
                cout << "succeed to free memory" << p << endl;
            }
        }
    }
};

char Test::m_buffer[sizeof(Test) * Test::count] = {0};
char Test::m_map[Test::count] = {0};

int main(int argc, char *argv[])
{
    Test* p = new Test;
    delete p;
    return 0;
}

指定地址创建对象

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class Test
{
    static  int m_count;
    static char* m_buffer;
    static char* m_map;
    int m_value;

public:
    static bool setMemorySource(char* memory, unsigned int size)
    {
        bool ret = false;
        m_count = size / sizeof(Test);
        ret = ( m_count && (m_map = reinterpret_cast<char*>(calloc(m_count, sizeof(char)))));

        if( ret )
        {
            m_buffer = memory;
        }
        else
        {
            free(m_map);
            m_map = NULL;
            m_buffer = NULL;
            m_count = 0;
        }
        return ret;
    }

    void* operator new ( unsigned int size)
    {
        void* ret = NULL;
         if( m_count > 0)
         {
             for( int i=0; i < m_count; i++)
             {
                 if( !m_map[i])
                 {
                     m_map[i] = 1;
                     ret = m_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(m_count > 0)
            {
                char* mem = reinterpret_cast<char*>(p);
                int index = (mem - m_buffer) / sizeof(Test);
                int flag = (mem - m_buffer) % sizeof(Test);
                if( (flag == 0) && (0 <= index) && (index < m_count) )
                {
                    m_map[index] = 0;
                    cout << "succeed to free memory:" << p << endl;

                }
            }
            else
            {
                free(p);
            }
        }
    }

    Test() {}
};

 int Test::m_count = 0;
char* Test::m_buffer = NULL;
char* Test:: m_map = NULL;

int main()
{
    char buffer[12] = {0};
    Test* pp  = new Test;
    cout <<"&buffer:  "<< &buffer << endl;
    cout << "pp:  " << pp <<endl;
    delete pp;

    Test::setMemorySource(buffer,sizeof(buffer));

    Test* p = new Test;
    delete p;
}

动态数组的内存管理

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class Test
{
    int m_value;
public:
    Test()
    {
        m_value = 0;
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值