std::set_new_handler
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <new>
|
||
| std::new_handler set_new_handler(std::new_handler new_p) |
||
使
new_p新的全球新的处理函数,并返回以前安装的新的处理程序.Original:
Makes
new_p the new global new-handler function and returns the previously installed new-handler.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 注释
。其目的是三件事情之一Original:
The new-handler function is the function called by
分配的功能</div> whenever a memory allocation attempt fails. Its intended purpose is one of three things:
Original:
allocation functions
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
使更多的内存可用
2) Original:
make more memory available
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
终止程序(例如,通过调用std::terminate)
3) Original:
terminate the program (e.g. by calling std::terminate)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
抛出异常的类型std::bad_alloc或来自std::bad_alloc
Original:
throw exception of type std::bad_alloc or derived from std::bad_alloc
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
如果“新的处理程序”回报,分配函数重复先前分配失败的尝试,并调用新的“处理程序”,如果分配失败,再次。结束循环,新的处理程序“,可致电std::set_new_handler(nullptr):如果分配失败尝试后,发现,std::get_new_handler返回一个空指针值分配功能,它会抛出std::bad_alloc.
Original:
If new-handler returns, the allocation function repeats the previously-failed allocation attempt and calls the new-handler again if the allocation fails again. To end the loop, new-handler may call std::set_new_handler(nullptr): if, after a failed allocation attempt, allocation function finds that std::get_new_handler returns a null pointer value, it will throw std::bad_alloc.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
在程序启动时,“新的处理程序”是一个空指针.
Original:
At program startup, new-handler is a null pointer.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 参数
| new_p | - | 的的类型std::new_handler,或空指针指向函数
Original: pointer to function of type std::new_handler, or null pointer The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[编辑] 返回值
以前安装的新的处理程序,或一个空指针,如果没有安装.
Original:
The previously-installed new handler, or a null pointer value if none was installed.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 例外
[编辑] 为例
#include <iostream> #include <new> void handler() { std::cout << "Memory allocation failed, terminating\n"; std::set_new_handler(nullptr); } int main() { std::set_new_handler(handler); try { while(true) new int[100000000ul]; }catch(const std::bad_alloc& e) { std::cout << e.what() << '\n'; } }
Output:
Memory allocation failed, terminating std::bad_alloc
[编辑] 另请参阅
| 分配的功能 Original: allocation functions The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数) | |
| set_new_handler |
注册一个新的处理程序 Original: registers a new handler The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数) |
| 新的处理程序的函数指针类型 Original: function pointer type of the new handler The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (的typedef) | |
| 内存分配失败时抛出的异常 Original: exception thrown when memory allocation fails The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类) | |