on

xiaoxiao2021-03-06  38

_SET_NEW_HANDLER The statement in the MSDN is _PNH _SET_NEW_HANDLER (_PNH PNewHandler);

_SET_NEW_HANDLER In MSDN Description: Transfer Control to Your Error-Handling Mechanism if The New Operator Fails To Allocate Memory. Translate is: If the new operator assigns memory fail, turn to _SET_NEW_HANDLER to deal with the error handling mechanism specified .

_SET_NEW_HANDLER has a parameter: _PNH PNewHandler, _PNH declaration is: typedef int (__cdecl * _PNH) (SIZE_T); Description PnewHandler is a pointer pointing to a function entry, which returns a integer value. If you returns 0, it means that the NEW operator does not try to allocate memory, return non-0, indicating that telling the New operator to continue attempting to assign memory.

_SET_NEW_HANDLER has a return value, returns the entry pointer of the original NewHandler function.

An instance of _SET_NEW_HANDLER: This instance can be expected to reserve some memory when the first new operator assignments fail, let the New operator redistribute, but when the second assignment fails, exit.

...... gpszTempMemory = new char [RESERVED_MEMORY_SIZE]; ...... int ProcOutOfMemory (size_t size) {if (gpszTempMemory) // first time, we use our reservation {delete [] gpszTempMemory; gpszTempMemory = NULL; Return 1;} else // second Time, Game over ~~~ {return 0;}} ... _SET_NEW_HANDLER (ProcoutofMemory); ......

转载请注明原文地址:https://www.9cbs.com/read-56988.html

New Post(0)