`

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

    博客分类:
  • c++
c++ 
阅读更多

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. 

 

 

 

 

 

分享到:
评论

相关推荐

    C++ 内存管理

    void * operator new[ ](size_t size); void operator delete[ ](void *p); // .. other members here .. }; void *TestClass::operator new[ ](size_t size) { void *p = malloc(size); return (p); } void Test...

    小型售货机

    void Delete_node(Good *pr,Good *p); //------------------------删除货物信息模块-----------------// void Delete_info(Good *p); #endif //--------------------Goodclass.h--------------------// //---------...

    使用Qt实现的C++画板(包含源码,可执行exe文件)

    void MainWindow::on_linecolor_r_slider_actionTriggered(int action) { } //这个函数就是用来保存的功能实现: void MainWindow::on_save_button_clicked() { ofstream out("D://data.txt", ios::out|ios::app); ...

    c++简单设计界面

    用C++写的一个简单的界面演示系统 void CMiniDrawDoc::AddFigure (CFigure *PFigure) { m_FigArray.Add (PFigure); SetModifiedFlag (); } CFigure *CMiniDrawDoc::GetFigure (int Index) { if (Index || ...

    C++ new、delete(new[]、delete[])操作符重载需要注意的问题

    new、delete(new[]、delete[])操作符的...void* operator new(size_t size) throw(std::bad_alloc); // 这里的 size 为分配的内存的总大小 void* operator new[](size_t size) throw(std::bad_alloc);   void ope

    17~C++ new和delete运算符重载

    重载 class A的new/delete 以及 new[]/delete[]运算符: 成功返回非空指针,失败返回空指针 #include #include using namespace std;... static void* operator new (size_t size) { void* pv = malloc

    C++实现mySQL接口

    开源的My sql连接接口,用C++,OrzMySQL::OrzMySQL() : m_impl(new OrzMySQLImpl) { } OrzMySQL::~OrzMySQL() { if (m_impl) { delete m_impl; } } DBId OrzMySQL::getId() const { return m_impl->getId();...

    C++之CNoTrackObject类和new delete操作符的重载实例

    本文实例讲述了C++中CNoTrackObject类和new delete操作符的... void* operator new(size_t nSize);   void operator delete(void*);   virtual ~CNoTrackObject(){}  }; 实现方法如下: 代码如下:void* CNoTra

    细说C++中的new与delete

    C++中内存的动态分配与管理永远是一个让C++开发者头痛的问题,本文...  void * ::operator new(size_t); //Global  void * class-name::operator new(size_t); //Class  上面是C++中operator new function的原型

    c++ 面试题 总结

    C++面试题 1.是不是一个父类写了一个virtual 函数,如果子类覆盖它的函数不加virtual ,也能实现多态? virtual修饰符会被隐形继承的。 private 也被集成,只事派生类没有访问权限而已 virtual可加可不加 子类的...

    操作系统进程控制

    void change_add(int bl_num,int me_s,int lo_ad) { int b,c; yh=lo_ad/me_s; b=lo_ad%me_s; c=bl_num*me_s+b; cout页号和偏移量:"<<yh<<"---"; cout物理地址为:"; } void init_page(p_ptr &l,m_ptr &k) { ...

    wangyue1.rar_ON_MESSAGE_On Message_VK_DELETE_ctrl alt delete_组合键

    包含C++类CCADMgr头文件,如:#include "CADMgr.h" 2: 定义变量如:CCADMgr m_cadMgr 3: 设置Ctrl+Alt+Del消息通知窗口,如:m_cadMgr.SetTargetWnd(m_hWnd) 4: 禁用或启用,如:m_cadMgr.DisableCAD(m_...

    c++航班订票程序,请多指教

    #include #include #include #include #define m 4 //3架飞机 #define n 5 //每架飞机5张票 ...void search_delete(int x); void write_to_file(); void show_wait(); bool comp(node *x,node*y);

    C++中new与delete、malloc与free应用分析

    一般来说,在C/C++的面试时,对于new/delete和malloc/free这两对的使用和区别经常被考查到,如果这种基础的问题都答不上来,估计很难过面试了。本文即是对new/delete和malloc/free这两对...void* operator new ( size_t

    C++第一次大作业

    这个作业将让你去练习建立一些简单的类和使用C++的基本功能,包括:封装,引用,动态内存开辟, 简单构造函数和析构函数和const。 下面给的程序片段未经过编译或调试. 做出合理的错误修正是你任务的一部分。 一般的,...

    学生管理信息系统 c++

    template <class T > void SeqList<T>::Insert(int i, T x) { int j; if (length>=MaxSize) throw "上溢"; if (i|| i>length+1) throw "位置错误"; for (j=length; j>=i; j--) data[j]=data[j-1]; //注意第j个...

    C/C++动态分配与释放内存的区别详细解析

    1. malloc()函数1.1 malloc的全称是memory allocation,中文叫动态内存分配。原型:extern void *malloc(unsigned int num_bytes); 说明:分配长度为num_bytes字节的内存块。...C,C++规定,void* 类型可以强

    餐厅管理信息系统课程设计

    工程使用的是基于对话框的程序,每个对话框为一个类,父类为CDialog,实现的一些功能都封装在类的函数中,体现了面向对象编程语言C++的特性。 导入ADO接口:在工程的stdafx.h文件里直接引入符号#import引入ADO库文件...

    C生成静态库给C++调用出现undefined reference to ...解决方案_cpp

    C生成静态库给C++调用出现undefined reference to ...解决方案 已经写好makefile,还有头文件,看注释,基本没问题的

    c++追赶法代码

    void get_l(); void get_y(); void get_x(); ~chasing(); }; void main() { chasing zgf; zgf.get_l(); zgf.get_y(); zgf.get_x(); } chasing::chasing() { cout请输入方程个数:"; cin>>m; a=new ...

Global site tag (gtag.js) - Google Analytics