走进C++程序世界-----operator new delete 重载
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了走进C++程序世界-----operator new delete 重载,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2666字,纯文字阅读大概需要4分钟。
内容图文
![走进C++程序世界-----operator new delete 重载](/upload/InfoBanner/zyjiaocheng/1144/ac3aa3e4aa55413cacfd2a046f3f4212.jpg)
?在C++ 的世界里,new 和delete 是keyword。而在C的世界里相相应的malloc和free是函数,关键C++的new和delete分析,详见前面的章节。这里就不在过多的介绍了。链接。
以下来研究下关于new 和delete的重载。
?1、对照使用重载和未使用重载
?未使用“
/*File : operator_new.cpp *Auth : sjin *Date : 2014-04-27 *Mail : 413977243@qq.com */ #include <iostream> using namespace std; class test { public: test(){cout << "*****构造test()*****"<< endl;}; ~test(){cout << "+++++析构test()+++++"<< endl;}; }; int main() { test * x = new test;//运行分配空间,再运行析构函数 delete x;//先运行析构函数,在释放空间 }
使用重载后,
/*File : operator_new.cpp *Auth : sjin *Date : 2014-04-27 *Mail : 413977243@qq.com */ #include <iostream> using namespace std; char mem[10000] = {‘\0‘}; int pos = 0; class test { public: test(){cout << "*****构造test()*****"<< endl;}; ~test(){cout << "+++++析构test()+++++"<< endl;}; public: void * operator new(size_t bytes){ cout << "------new test()------" << endl; int alloc = pos; pos += bytes; return (mem + alloc); }; void operator delete(void *){ cout << "------delete test()------" << endl; }; }; int main() { test * x = new test; delete x; }
![技术分享图片](/upload/getfiles/default/2022/11/5/20221105105529775.jpg)
/*File : operator_new.cpp *Auth : sjin *Date : 2014-04-27 *Mail : 413977243@qq.com */ #include <iostream> using namespace std; char mem[10000] = {‘\0‘}; int pos = 0; class test { public: test(){cout << "*****构造test()*****"<< endl;}; ~test(){cout << "+++++析构test()+++++"<< endl;}; public: void * operator new(size_t bytes){ cout << "------new test()------" << endl; int alloc = pos; pos += bytes; return (mem + alloc); }; void operator delete(void *){ cout << "------delete test()------" << endl; }; }; int main() { test * x = new test; delete x; x = new test[3]; delete [] x;//这里对数组释放,须要注意 }
![技术分享图片](/upload/getfiles/default/2022/11/5/20221105105529975.jpg)
#include <iostream> using namespace std; char mem[10000] = {‘\0‘}; int pos = 0; class test { public: test(){cout << "*****构造test()*****"<< endl;}; ~test(){cout << "+++++析构test()+++++"<< endl;}; public: void * operator new(size_t bytes){ cout << "------new test()------" << endl; int alloc = pos; pos += bytes; return (mem + alloc); }; void operator delete(void *){ cout << "------delete test()------" << endl; }; void * operator new[](size_t bytes){ cout << "------new test()------" << endl; int alloc = pos; pos += bytes; return (mem + alloc); }; void operator delete[](void *){ cout << "------delete test()------" << endl; }; }; int main() { test * x = new test; delete x; x = new test[3]; delete [] x; }
![技术分享图片](/upload/getfiles/default/2022/11/5/20221105105530160.jpg)
对new 和delete 函数的重载能够用来检測 内存泄露的情况。
?
?
?
原文:https://www.cnblogs.com/zhchoutai/p/8685569.html
内容总结
以上是互联网集市为您收集整理的走进C++程序世界-----operator new delete 重载全部内容,希望文章能够帮你解决走进C++程序世界-----operator new delete 重载所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】