3 回答

TA貢獻1859條經驗 獲得超6個贊
第二部分
operator new
new_handler
必
提供更多內存:
安裝不同的新處理程序:set_new_handler
卸載新的處理程序:set_new_handler
operator new
std::bad_alloc
拋出異常std::bad_alloc
operator new
不返回:abort
exit
.
new_handler
set_new_handler
operator new
set_new_handler
set_new_handler
operator new
new_handler
& set_new_handler
要求4(增強):operator new
null
operator new
第3.7.4.1.3節:
未能分配存儲的分配函數可以調用當前安裝的 new_handler
(18.4.2.2
(如有的話)。[注意:程序提供的分配函數可以獲得當前安裝的地址。 new_handler
使用 set_new_handler
功能( 18.4.2.3
)]如果使用空異常聲明的分配函數-規范( 15.4
),throw()
無法分配存儲,它將返回一個空指針。未分配存儲的任何其他分配函數只能通過拋出類的異常來指示失敗。 std::bad_alloc
(18.4.2.1
)或派生自 std::bad_alloc
.
new operator
:
void * operator new(std::size_t size) throw(std::bad_alloc){ // custom operator new might take additional params(3.7.3.1.1) using namespace std; if (size == 0) // handle 0-byte requests { size = 1; // by treating them as } // 1-byte requests while (true) { //attempt to allocate size bytes; //if (the allocation was successful) //return (a pointer to the memory); //allocation was unsuccessful; find out what the current new-handling function is (see below) new_handler globalHandler = set_new_handler(0); set_new_handler(globalHandler); if (globalHandler) //If new_hander is registered call it (*globalHandler)(); else throw std::bad_alloc(); //No handler is registered throw an exception }}
- 3 回答
- 0 關注
- 406 瀏覽
添加回答
舉報