【C++】69.自定义内存管理

问题:

new关键字创建出来的对象位于什么地方?

这个两个是操作符,意味着可以进行操作符重载。也可以改变内存分配方式!

操作符重载注意:

1.不推荐全局重载

2.建议针对具体的类进行局部重载

3.针对类的成员函数,重载new,delete这样的函数默认是静态成员函数

//静态存储区中创建动态对象
class Test
{
   static const unsigned int COUNT = 4;
   static char c_buffer[];   // 字节数组
   static char c_map[];      // 字节数组,标记是否存在对象存在
    
   int m_value;

public:  
   void* operator new(unsigned int size)
   {
       void* ret = NULL;
       
       for(int i=0; i<COUNT; i++)
       {
           if( !c_map[i])
           {
               c_map[i] = 1;  // the mem
               
               ret = c_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 - 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" << endl;
          }
      }     
   }
};  

char Test::c_buffer[sizeof(Test) * Test::COUNT] = {0};
char Test::c_map[Test::COUNT] = {0};
                 
int main()
{
   cout <<  "==== Test signal Object === " << endl; 
   Test* p = new Test;
   
   delete p;
   
   cout << endl;
   
   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;
}

 

问题:

如何在指定的地址上创建C++ 对象??

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

 

再议论 new[] delete[]:

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、付费专栏及课程。

余额充值