new_handler & set_new_handler

//the discriptions in msdn ,we can find this document in

//website:http://msdn.microsoft.com/library/default.asp?url=/library/en-//us/vcstdlib/html/vclrf_new_setnewhandler.asp

Return Value

0 on the first call and the previous new_handler on subsequent calls.

Remarks

The function stores _Pnew in a static new handler pointer that it maintains, then returns the value previously stored in the pointer. The new handler is used by operator new(size_t).

Example
// new_set_new_handler.cpp
// compile with: /EHsc
#include<new>
#include<iostream>

using namespace std;
void __cdecl newhandler( )
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //__cdecl程序的压栈方式为C风格
//__stdcall为PASCAL风格
//举个例子:
//(1) C函数 Fun1(a,b,c)
// 函数调用时,参数压栈顺序为 c , b , a
//(2) PASCAL函数 Fun(a,b,c)
// 函数调用时,参数压栈顺序为 a, b , c { cout << "The new_handler is called:" << endl; throw bad_alloc( ); return; } int main( ) { set_new_handler (newhandler); try { while ( 1 ) { new int[5000000]; cout << "Allocating 5000000 ints." << endl; } } catch ( exception e ) { cout << e.what( ) << " xxx" << endl; } }
Sample Output
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
The new_handler is called:
bad allocation

以上例子程序在VC6.0环境下不能编译通过,但在dev_c++中可以通过编译,虽然没试过但相信在gcc下同样可以实现。如果在VC6.0环境下编译,那么必须用_set_new_handler而非set_new_handler, 下面是一个c风格的使用例子。

Example

/* HANDLER.CPP: This program uses _set_new_handler to 
 * print an error message if the new operator fails.
 */

#include <stdio.h>
#include <new.h>

/* Allocate memory in chunks of size MemBlock. */
const size_t MemBlock = 1024;

/* Allocate a memory block for the printf function to use in case
 * of memory allocation failure; the printf function uses malloc.
 * The failsafe memory block must be visible globally because the
 * handle_program_memory_depletion function can take one 
 * argument only.
 */
//非常值得注意的是在这个地方,当内存分配时如果你需要打印提示信息,而打印的步骤也是需要内存的,为防止
//连打印错误信息所需的内存都没有的窘态,必须事先先开辟一块内存以备不时之需,在内存不足需打印信息时可先放掉
//这块预留内存,这就是failsafe的用处所在。具体的请参考msdn中关于_set_new_handler的说明,以及Scott Mayers
在effective c++ 2e iterm7中相关的说明
char * failsafe = new char[128];

/* Declare a customized function to handle memory-allocation failure.
 * Pass this function as an argument to _set_new_handler.
 */
int handle_program_memory_depletion( size_t );

void main( void )
{
   // Register existence of a new memory handler.
   _set_new_handler( handle_program_memory_depletion );
   size_t *pmemdump = new size_t[MemBlock];
   for( ; pmemdump != 0; pmemdump = new size_t[MemBlock] );
}

int handle_program_memory_depletion( size_t size )
{
   // Release character buffer memory.
   delete failsafe;
   printf( "Allocation failed, " );
   printf( "%u bytes not available.\n", size );
   // Tell new to stop allocation attempts.
   return 0;
}

Output

Allocation failed %0 bytes not available.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值