Effective C++ ????????
???????????? ???????[ 2012/12/12 10:57:50 ] ????????
??????????治???????
????operator new?????????????????????????????????????????????0???Щ????????????????????????????????????????????ó?????????????????????????????????β????????????????????????治?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????У????new???????????????
????????????????????????????????????????????????·?????????????????磬c????????????????????????????????????????沢?????????????????c++??????????????????????????
#define new(ptr?? type)
try { (ptr) = new type; }
catch (std::bad_alloc&) { assert(0); }
??????“????std::bad_alloc?????????”??????bad_alloc??operator new???????????????????????????????std??bad_alloc?????????????????28?????????“???”?????????“assert?????????”??????????c????<assert.h>?????????????????????????汾<cassert>????????49????????assert???????????鴫????????????????????????????????????????????????abort??assert??????????????ndebug???????????????2???????????????????£?????????ndebug?????assert??????????????????????????????????????????????assertion??????
????new??????????????????????????assert????????????????????????????????κ?????????????治?????????????????????c++???????????????????п????new?и??????????÷???????磬????????t??????????????????????????????????????????????????????д????
new t;
new t(constructor arguments);
new t[size];
?????????????????????????????????????壨?????operator new???????????????????????new?????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????棬????operator new??????????????????????????????????????????????????——?????new-handler????????operator new????????????????Щ?????????8??
??????????????????????set_new_handler??????????????<new>???????????????????????
typedef void (*new_handler)();
new_handler set_new_handler(new_handler p) throw();
?????????????new_handler???????????????????????????????????????????з???????????set_new_handler???????????????new_handler??????????
????set_new_handler???????????operator new??????????????????????????????????????set_new_handler???????????????????????????????????
?????????????????????set_new_handler??
// function to call if operator new can't allocate enough memory
void nomorememory()
{
cerr << "unable to satisfy request for memory
";
abort();
}
int main()
{
set_new_handler(nomorememory);
int *pbigdataarray = new int[100000000];
...
}
????????operator new?????100??000??000????????????nomorememory???????????????????????????????????????????????????????????????????á??????????£?????cerr??д????????????????????????棬?????????...??
????operator new?????????????????????new-handler?????????????Σ?????????????????????????檔????????????????????8???????????????????????????????????????????ú??new-handler??????????????湦???е?????
??????????????????檔???operator new????η???????????п????ó??????????????????????????????????????????????飬????????ε???new-handler????????????????Щ?????????????????????????????′????????????????????и?????????
?????? ?????????????new-handler??????????????new-handler?????????????????????棬????????????????new-handler??????????????????????????????????new-handler???????????new-handler????????????????set_new_handler?????????operator new????new-handler???????y???????????????????????????????????new-handler??????????????????????????′ε???????????????????????????new-handler?????????Щ????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11