zoukankan      html  css  js  c++  java
  • 重载new delete操作符是怎么调用的

    自定义的new操作符是怎么对英语new 一个对象的?自定义的delete操作符什么情况下得到调用?new一个对象时出现异常需要我操心内存泄露吗?下面的一个例子帮我们解开所有的疑惑。

    1. 调用规则  new(A,B) class(param)  -> operator new(sizeof(class), A, B)

    2. 自定义的操作符只用new对象异常的时候才会得到调用机会,而且调用哪个delete和你用的那个new一一对应,

    规则是 new(X, Y) class(param);  -> delete(X, Y) class;-> operator delete(X, Y, void* p)

    3. 我们不用担心new对象时候引发的异常的内存泄露,c++系统会我们在异常发生的第一时间(哪怕我们定义了异常处理函数)帮助我们释放内存。


    /*
    运行结果:
    Normal operator new called.
    Normal operator delete called.
    
    Custom operator new called.
    Normal operator new called.
    Normal operator delete called.
    
    Normal operator new called.
    Normal operator delete called.
    exception
    
    Custom operator new called.
    Normal operator new called.
    Custom operator delete called.
    Normal operator delete called.
    exception
    
    请按任意键继续. . .
    */
    
    #include <cstddef>
    #include <new>
    #include <iostream>
    using namespace std;
    
    void * operator new(std::size_t sz)
    	throw(std::bad_alloc)
    {
    	std::cout << "Normal operator new called." << std::endl ;
    
    	void * p = std::malloc(sz) ;
    	if (!p)
    		throw std::bad_alloc() ;
    	return p ;
    }
    
    void operator delete(void * p) throw()
    {
    	std::cout << "Normal operator delete called." << std::endl ;
    	if (p)
    		std::free(p) ;
    }
    
    void * operator new(std::size_t sz, std::ostream & out)
    	throw(std::bad_alloc)
    {
    	out << "Custom operator new called." << std::endl ;
    	return ::operator new(sz) ;
    }
    
    void operator delete(void * p, std::ostream & out) throw()
    {
    	out << "Custom operator delete called." << std::endl ;
    	::operator delete(p) ;
    }
    
    class T
    {
    public:
    	T(bool should_throw) { if (should_throw) throw 1 ; }
    } ;
    
    int main()
    {
    	// Calls normal new, normal delete.调用标准的new、delete
    	T * p = new T(false) ;
    	delete p ;
    	std::cout << std::endl ;
    
    	// Calls custom new, normal delete.  调用自定义的new, 标准的delete
    	//调用规则  new(A,B) class(param)  -> operator new(sizeof(class), A, B)
    	p = new(std::cout) T(false) ;
    	delete p ;
    	std::cout << std::endl ;
    
    	// Calls normal new, normal delete.  调用标准的new、delete 而且抛出异常
    	try
    	{
    		T * p = new T(true) ;
    		cout << "accident happens, should not get here" << endl;
    		//上面语句,没打印,说明不是代码调用了delete,而是c++框架自动为我们掉delete,即new失败时系统会自动为我们调用delete函数
    		delete p ;
    	}
    	catch (...)
    	{
    		cout << "exception" << endl;
    	}
    	std::cout << std::endl ;
    
    	// Calls custom new, custom delete. 调用自定义的new、delete,这个使用我们自定义的delete会得到调用
    	// 规则是 new(X, Y) class(param);  -> delete(X, Y) class;
    	try
    	{
    		T * p = new(std::cout) T(true) ;
    		cout << "accident happens, should not get here" << endl;
    		delete p ;
    	}
    	catch (...)
    	{
    		cout << "exception" << endl;
    	}
    	std::cout << std::endl ;
    }


  • 相关阅读:
    使用Spring的ReloadableResourceBundleMessageSource读取properties配置
    IFrame自适应高度
    Js返回页面顶部
    复制页面内容时添加额外信息
    怎样将Excel中的日期格式转换为文本格式
    为Tomcat页面设置访问权限(HTTP)
    为tomcat页面设置访问权限(BASIC认证)
    离线安装Maven FOR Eclipse插件
    sharepoint 获得上级和部门的封装函数
    SharePoint定制开发个性皮肤
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3315603.html
Copyright © 2011-2022 走看看