c++ - delete(void *, size_t) or delete(void *)

In my previous dicuss, we have seen the example of overloaded delete and new operator. and as a matter of fact, we have seen the following signature of the delete operator. 

 

 

void operator delete(void *);
void operator delete(void *, size_t size);

 

 

As you will soon find out that they both match the same new operator

 

void operator new (size_t);

 

but which one should we  choose to use?

 

and even, you can define both the operator delete(void *) and the operator delete (void *, size_t) in the same class, and according to my test , in the placement operator overloading example, the "operator delete(void *)" has precedance over the "operator delete (void *, size_t)"....

 

so here is my rule of thme, we uses operator delete (void *, size_t size) when 

 

  • the size_t is not barely the size of the object (this could happen if the new operator has say, preallocate some space which is larger than what is required by the object itself.)
  • the size_t is necessary to determine flows or it is necessary in the clean up that the delete operator is supposed to do.
  • Booking and logging requirement.
  • This can be thought as an extension to the point 2, where you can normally see allocated memory is larger than the object itself, is when you allocate a array (new [] operator actually does this)....

 

And when to use the operator delete (void *)?

 

  • when the size of the pointer in the first parameter is exact the size of the object
  • The size_t size is irrelevant in the deallocation flow. 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值