关闭

operator new function & operator delete function

215人阅读 评论(0) 收藏 举报

Process 

operator new function:

1. Invoking operator new standard library function, achieve efficient space  that  hasn't type define.

2. Run the current class constructor to initializing objects member(data member).

3. Return pointer which point to new objects address.


operator delete function

1 . Run destructor of object.

2. Release memory of this object obtain by invoking operator delete function in the standard library .


operator new and operator  has two different version:

  • void *operator new (size_t  n); // allocate an object
  • void *operator new[] (size_t n); //allocate an array
  • void *operator delete (void *); // free an object

  • void *operator delete[] (void *); //free an array

The relationship between the "operator new/operator delete" and "allocate":


Type *  newelements = alloc.allocate(newcapacity)  ======equivalent===== Type * newelements = static_cast<Type *>(operator new[](newcapacity * sizeof(Type)));

alloc.deallocate(elements , end - elements );             ======equivalent=====  operator delete[]( elements )


The different between the "operator new/operator delete" and "allocate":


 operator new/ operator delete : To operate the "void *" objects.

allocate: Operating a real object of class type.

According to this different character between "new/delete" and "allocate" , we can't to calculate size of memory and can't to using forced conversion.


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6077次
    • 积分:158
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章分类
    文章存档