New Handler

1.new handler

   (1)当operator new 没能力为你分配出你所申请的memory,会抛一个std::bad_alloc exception。某些老编译器则是返回0---你仍然可以令编译器那么做:

             new (nothrow) Foo;

             此为nothrow形式

   (2)抛出exception之前会先(不止一次)调用一个可由client指定的handler,以下是new handler的形式和设定方法:

             typedef void (*new_handler)();

             new_handlerset_new_handler(new_handler p) throw();

   (3)设计良好的new handler只有两种选择:

            [1]更多memory可用

            [2]调用abort()或exit()、

   (4)

            void* operator new(size_t size, const std::nothrow_t&)

                                                _THROW0()

            {
                   //try to allocate size bytes
                   void* p;
                   while((p = malloc(size)) == 0) {
                           //buy more memory or return null pointer
                  _TRY_BEGIN
                           if( _callnewh(size) == 0) break;
                  _CATCH(std::bad_alloc) return (0);
                  _CATCH_END
                  }
                  return (p);

          }

 (5)示例

   

#include <iostream>
#include <new>
#include <cassert>

using namespace std;


void noMoreMemory() {
	cout << "Out of memory";
	abort();
}


int main() {
	set_new_handler(noMoreMemory);
	
	int* p = new int[100000000000000000];
	assert(p);

	p = new int[10000000000000];
	assert(p);
	
	return 0;
}


本例中的new handler中若无调用abort(),执行后cerr会不断出现"out of memory",需强制中断。这样的表现是正确的,表示当operator new无法满足申请量时,会不断调用new handler直到获得足够memory

(6)new_handler __cdecl set_new_handler(

                                            new_handlernew_p)

           {

                     //cannot use stub to register a new handler

                    assert(new_p == 0);

                    //remove current handler

                    _set_new_handler(0);

                    return 0;

           }

2.=default,=delete

  class Foo {

  public:

            Foo() = default;

            Foo(const Foo&) = default;

            Foo& operator=(const Foo&) = delete;

            ~Foo() = default;

  };

  it is not only for constructors and assignments,but also applies to operator new/new[],operator delete/delete[] and their overloads.

#include <iostream>
using namespace std;


class Foo {
public:
	long L;
public:
	Foo() {
	}
	Foo(long l): L(l) {
	}
	//static void* operator new(size_t size) = default;
	//error: 'static void* Foo::operator new(size_t)' cannot be defaulted
	//static void operator delete(void* pdead, size_t size) = default;
	//error: 'static void Foo::operator delete(void*, size_t)' cannot be defaulted
	static void* operator new[](size_t size);
	static void operator delete[](void* pdead, size_t size);
};


class Goo {
public:
	long L;
public:
	Goo() {
	}
	Goo(long l): L(l) {
	}
	
	static void* operator new(size_t size) = delete ;
	static void operator delete(void* pdead, size_t size) = delete ;
};



int main() {
	Foo* p1 = new Foo(5);
	delete p1;
	
	//Foo* pF = new Foo[10];
	//delete[] pF;
	
	//Goo* p2 = new Goo(7);
	//delete p2;
	
	Goo* pG = new Goo[10];
	delete[] pG;
	
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值