std::set_new_handler

来自cppreference.com
< cpp | memory | new

 
 
实用工具库
类型的支持 (basic types, RTTI, type traits)
动态内存管理
错误处理
程序实用工具
可变参数函数
日期和时间
函数对象
initializer_list(C++11)
bitset
hash(C++11)
关系运算符
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
双和元组
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
掉期,远期和移动
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
动态内存管理
低级别的内存管理
分配器
Original:
Allocators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
allocator
allocator_traits(C++11)
allocator_arg_t(C++11)
allocator_arg(C++11)
uses_allocator(C++11)
scoped_allocator_adaptor(C++11)
未初始化的存储空间
Original:
Uninitialized storage
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
uninitialized_copy
uninitialized_copy_n(C++11)
uninitialized_fill
uninitialized_fill_n
raw_storage_iterator
get_temporary_buffer
return_temporary_buffer
智能指针
Original:
Smart pointers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr(C++11)
shared_ptr(C++11)
weak_ptr(C++11)
auto_ptr(过时了)
owner_less(C++11)
enable_shared_from_this(C++11)
bad_weak_ptr(C++11)
default_delete(C++11)
垃圾收集的支持
Original:
Garbage collection support
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declare_reachable(C++11)
undeclare_reachable(C++11)
declare_no_pointers(C++11)
undeclare_no_pointers(C++11)
pointer_safety(C++11)
get_pointer_safety(C++11)
杂项
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pointer_traits(C++11)
addressof(C++11)
align(C++11)
C库
Original:
C Library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
低级别的内存管理
功能
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator new
operator new[]
operator delete
operator delete[]
get_new_handler(C++11)
set_new_handler
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
bad_alloc
bad_array_new_length(C++11)
nothrow_t
类型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
new_handler
对象
Original:
Objects
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
nothrow
 
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.

目录

[编辑] 注释

。其目的是三件事情之一
Original:
The new-handler function is the function called by
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
使更多的内存可用
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.
2)
终止程序(例如,通过调用std::terminate
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.
3)
抛出异常的类型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.
如果“新的处理程序”回报,分配函数重复先前分配失败的尝试,并调用新的“处理程序”,如果分配失败,再次。结束循环,新的处理程序“,可致电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.
在程序启动时,“新的处理程序”是一个空指针.
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.

[编辑] 参数

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.

[编辑] 例外

noexcept specification:  
noexcept
  (C++11 起)

[编辑] 为例

#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.

(函数) [edit]
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.

(函数) [edit]
新的处理程序的函数指针类型
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) [edit]
内存分配失败时抛出的异常
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.

(类) [edit]