When operator new is unable to allocate the memory we requested, it will throw a std::bad_alloc exception. Some old compilers return 0, but modern compilers can still do this: new (nothrow) Foo;
Before the exception is thrown, a handler that can be specified by the client is called (more than once). The following is the form and setting method of the new handler:
typedef void (*new_handler)();
new_handler set_new_handler(new_handler p) throw();
There are only two options for a well-designed new_handler:
- Make more memory available
- Call
abort()
orexit()
void *operator_new(size_t size, const std::nothrow_t &_THROW0())
{
// try to allocate size bytes
void *p;
while ((p = malloc(size)) == 0)
{
// buy more memory or return null pointer
_TRY_BEGIN
if (_callnew_h(size) == 0) break;
_CATCH(std::bad_alloc) return 0;
_CATCH_END
}
return (p);
}
#include <new>
#include <iostream>
#include <cassert>
using namespace std;
void onMoreMemory()
{
cerr << "out of memory" << endl;
abort();
}
int main()
{
set_new_handler(onMoreMemory);
int *p = new int[10000];
assert(p);
cout << p << endl;
p = new int[100000000000000];
assert(p);
cout << p << endl;
return 0;
}
Output [gcc version 7.5.0]
0x55ad89dd7e70
out of memory
Aborted (core dumped)
In this example, if abort() is not called in the new handler, cerr will continue to appear "out of memory" after execution, and it needs to be interrupted forcibly. This behavior is correct. It means that when operator new cannot meet the application amount, it will keep calling new handler until enough memory is obtained.
default、delete
Default and delete are not only applicable to constructors (copy construction, move construction) and assignment functions (copy assignment, move assignment)
class Foo {
public:
Foo() = default;
Foo(const Foo&) = delete;
Foo& operator=(const Foo&) = delete;
~Foo() = default;
};
practical testing:
#include <iostream>
using namespace std;
class Foo {
public:
long _x;
public:
Foo(long x = 0) : _x(x)
{ }
// static void *operator new(size_t size) = default; // error: only special member functions may be defaulted
// static void operator delete(void *pdead, size_t size) = default; // error: only special member functions may be defaulted
static void *operator new[](size_t size) = delete;
static void operator delete[](void *pdead, size_t size) = delete;
};
int main()
{
// Foo *p1 = new Foo[10]; // error: call to deleted function 'operator new[]'
// delete [] p1; // error: attempt to use a deleted function
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。