<new>函式</new>
如需 Visual Studio 2017 的最新文件請參閱 Visual Studio 2017 文件。
nothrow | set_new_handler |
nothrow
提供可用來做為引數物件nothrow
版本新和刪除。
extern const std::nothrow_t nothrow;
備註
物件用於做為函式引數符合參數型別std::nothrow_t。
範例
請參閱new 運算子和new [] 運算子的範例將說明如何std::nothrow_t
做為函式參數。
set_new_handler
安裝時要呼叫的使用者函式operator new
中嘗試配置記憶體失敗。
new_handler set_new_handler(new_handler _Pnew) throw();
參數
_Pnew
若要安裝 new_handler。
傳回值
0 在第一次呼叫和前一個new_handler
的後續呼叫。
備註
函式存放區_Pnew
中靜態新處理常式指標,它會維護,然後傳回先前儲存在指標的值。 新的處理常式由new 運算子( size_t)。
範例
// new_set_new_handler.cpp
// compile with: /EHsc
#include<new>
#include<iostream>
using namespace std;
void __cdecl newhandler( )
{
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( ) << endl;
}
}
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