当前位置:网站首页>Article 49: understanding the behavior of new handler
Article 49: understanding the behavior of new handler
2022-06-13 04:56:00 【CCSUZB】
Consider the following code :
void outOfMem() {
std::cerr << "unable to satisfy request for memory\n";
std::abort();
}
int main() {
std::set_new_handler(outOfMem);
int *p = new int[1000000000000000]; // abnormal
return 0;
}
When operator new When a memory request cannot be satisfied , It's constantly calling new-handler function , Until you find enough memory .
Design a good new-handler The function must do the following
- Make more memory available
- Install another
new-handler - Removal
new-handler - Throw out
bad_alloc - No return
Sometimes you want to deal with memory allocation failures in different ways :
class X {
public:
static void outOfMemory();
};
class Y {
public:
static void outOfMemory();
};
X *p1 = new X; // If the assignment is not successful , call X::outOfMemory
Y *p1 = new Y; // If the assignment is not successful , call Y::outOfMemory
The solution is to make every class Provide your own set_new_hanlder and operator new:
class Widget {
public:
static std::new_handler set_new_handler(std::new_handler p) throw();
static void *operator new(std::size_t size) throw(std::bad_alloc);
private:
static std::new_handler currentHandler;
};
std::new_handler Widget::currentHandler = 0;
std::new_handler Widget::set_new_handler(new_handler p) throw()
{
std::new_handler oldHandler = currentHandler;
currentHandler = 0;
return oldHandler;
}
Last ,Widget Of operator new Do the following
- Call standard
set_new_hanlder, informWidgeError handling function - call
global operator new, Perform the actual memory allocation - If
global operator newBe able to allocate enoughWidgetMemory used by object ,WidgetOfoperator newWill return a pointer , Point to the distribution income
Implement the above operations with a resource management class :
class NewHandlerHolder
{
public:
explicit NewHandlerHolder(std::new_handler nh) : handler(nh){
} // Get the current new-hanlder
~NewHandlerHolder()
{
std::set_new_handler(handler);}
private:
std::new_handler handler;
NewHandlerHolder(const NewHandlerHolder&); // prevent copy
NewHandlerHolder & operator =(const NewHandlerHolder &);
};
Then implement Widget Of opeator new
void* Widget::operator new (std::size_t size) throw(std::bad_alloc)
{
NewHandlerHolder h(std::set_new_handler(currentHandler)); // install Widget Of new-handler
return ::operator new(size); // Allocate memory or throw an exception
}
The customer code is used as follows :
Widget::set_new_handler(outOfMem); // Set up outOfMem by Widget Of new-handing
Widget* pw1 = new Widget; // If memory allocation fails, call outOfmem
std::string* ps = new std::string; // If memory allocation fails, call global new-hanlding function
Widget::set_new_handler(0); // Set up Widget Of new-handing by null
Widget* pw2 = new Widget; // If memory allocation fails , Throw an exception immediately
边栏推荐
- Kaggle time series tutorial
- 【JS解决】leedcode 200. 岛屿数量
- Leetcode game 297 (20220612)
- Createanonymousthreadx passes parameters to anonymous threads
- 推荐的图片临时在线压缩工具
- Your one-on-one meetings are inefficient. You can do this!
- OJ problem solution summary
- Analysis of the principle of V-model and its application in user defined components
- promise处理js多线程全部获得结果后同一处理结果
- Converting MySQL data to PostgreSQL with Navicat
猜你喜欢

Embedded hardware - read schematic

SQL notes

Kaggle 时间序列教程

Colab tutorial (super detailed version) and colab pro/pro+ evaluation

Spice story

Shell built-in string substitution

Analysis on the similarities and differences of MVC, MVP and mvvc

QT direction key to move focus

QT client development -- driver loading problem of connecting to MySQL database

Kaggle time series tutorial
随机推荐
Basic syntax example for go
Third party comment plugin
Chinese trumpet creeper
2022年建筑架子工(建筑特殊工种)特种作业证考试题库及在线模拟考试
Section 6 - pointers
rust编程-链表:使用struct实现链表,使用堆合并k个升序链表,自定义Display
Advanced C language - Section 1 - data storage
Mysql database installation
Promise processing JS multithreads get the same processing result after all the results are obtained
OJ problem solution summary
Blockly learning ----2 Code generation, grid, scaling, events, storage
Hidden implementation and decoupling, knowing Pimpl mode
Section 3 - functions
[try to hack] upload labs (temporarily write to 12)
Tita: Xinrui group uses one-to-one talk to promote the success of performance change
RMQ、LCA
How to use redis
Optocoupler working principle function electric parameter application circuit
SQL notes
Section 8 - Practical commissioning techniques