new指针后,地址相同
发布日期:2021-06-30 22:17:13 浏览次数:2 分类:技术文章

本文共 4988 字,大约阅读时间需要 16 分钟。

前言

在linux程序中,会new一个会话类,如果不符合条件,会删掉。

在new和delete时,为了调试,打印这个类指针地址,用来辅助判断有多少个会话进来和销毁。
tail -f /var/log/messages时,看到大部分都是都是一个地址,心里一惊。
但是逻辑没错,难道一个指针用完,再new一个指针,用的还是空闲的那块地址?
这个知识点没注意过,今天验证了一下。

验证完的结论,内存管理程序会优先用空闲的那块内存,所以打印出的地址大部分是相同的。

linux和windows都是这样的。

所以,要跟踪一个new出的类指针,打印地址不靠谱,要打印类方法提供的信息才行。e.g. 给每个类加一个info方法,将需要的类信息打印出来。这样,创建和销毁的时候,就知道具体的类信息了。

实验

// @file main.cpp#include 
#include
#include
#include
#include
#include
#include "my_syslog.h"#ifndef SAFE_DELETE#define SAFE_DELETE(p) \ if (NULL != (p)) { \ delete (p); \ (p) = NULL; \ }#endif // #ifndef SAFE_DELETEvoid init();void uninit();int fn_test();int main(){ init(); fn_test(); uninit(); return EXIT_SUCCESS;}void init(){ ns_syslog::open_syslog("test_new"); // 设置控制变量中的日志条件, 实际应用中, 是从配置文件读取的控制开关 ns_syslog::g_log_condition.b_EMERG = false; ns_syslog::g_log_condition.b_CRIT = true; ns_syslog::g_log_condition.b_ALERT = true; ns_syslog::g_log_condition.b_ERR = true; ns_syslog::g_log_condition.b_WARNING = true; ns_syslog::g_log_condition.b_NOTICE = true; ns_syslog::g_log_condition.b_INFO = true; ns_syslog::g_log_condition.b_DEBUG = true; // 根据控制变量, 设置日志的mask // 在实际应用中, 这里可以是动态设置, e.g. 配置文件检测线程发现配置变了, 需要变更某些级别的日志记录结果 ns_syslog::set_log_level( ns_syslog::g_log_condition.b_EMERG, ns_syslog::g_log_condition.b_ALERT, ns_syslog::g_log_condition.b_CRIT, ns_syslog::g_log_condition.b_ERR, ns_syslog::g_log_condition.b_WARNING, ns_syslog::g_log_condition.b_NOTICE, ns_syslog::g_log_condition.b_INFO, ns_syslog::g_log_condition.b_DEBUG);}void uninit(){ ns_syslog::close_syslog();}int fn_test(){ int i_log_index = 0; char* psz_buf = NULL; char* psz_buf1 = NULL; MYLOG_D("%d. psz_buf addr = %p\n", ++i_log_index, psz_buf); MYLOG_D("........................................"); psz_buf = new char[1024]; if (NULL != psz_buf) { MYLOG_D("%d. psz_buf addr = %p\n", ++i_log_index, psz_buf); } SAFE_DELETE(psz_buf); psz_buf = new char[1024]; if (NULL != psz_buf) { MYLOG_D("%d. psz_buf addr = %p\n", ++i_log_index, psz_buf); } SAFE_DELETE(psz_buf); psz_buf = new char[1024]; if (NULL != psz_buf) { MYLOG_D("%d. psz_buf addr = %p\n", ++i_log_index, psz_buf); } SAFE_DELETE(psz_buf); psz_buf = new char[1024 + 1]; if (NULL != psz_buf) { MYLOG_D("%d. psz_buf addr = %p\n", ++i_log_index, psz_buf); } SAFE_DELETE(psz_buf); psz_buf = new char[1024 + 1]; if (NULL != psz_buf) { MYLOG_D("%d. psz_buf addr = %p\n", ++i_log_index, psz_buf); } SAFE_DELETE(psz_buf); psz_buf = new char[1024 + 1]; if (NULL != psz_buf) { MYLOG_D("%d. psz_buf addr = %p\n", ++i_log_index, psz_buf); } SAFE_DELETE(psz_buf); MYLOG_D("........................................"); psz_buf1 = new char[1024]; if (NULL != psz_buf1) { MYLOG_D("%d. psz_buf1 addr = %p\n", ++i_log_index, psz_buf1); } SAFE_DELETE(psz_buf1); psz_buf1 = new char[1024 + 1]; if (NULL != psz_buf1) { MYLOG_D("%d. psz_buf1 addr = %p\n", ++i_log_index, psz_buf1); } SAFE_DELETE(psz_buf1); // 以上psz_buf的地址都是一个值 MYLOG_D("........................................"); psz_buf = new char[1024 + 1]; if (NULL != psz_buf) { MYLOG_D("%d. psz_buf addr = %p\n", ++i_log_index, psz_buf); } psz_buf1 = new char[1024]; if (NULL != psz_buf1) { MYLOG_D("%d. psz_buf1 addr = %p\n", ++i_log_index, psz_buf1); } SAFE_DELETE(psz_buf); SAFE_DELETE(psz_buf1); // 同时使用一个以上的buf时,地址才不同. // so, on linux, 查看地址上带的内容是否为新内容,不能依赖打印buffer的地址,而要看地址中带的内容 // 看到不断的new和delete同一个地址,也不要惊:) // 在windows上,用同样的代码做过实验了,和linux实验结果相同 return EXIT_SUCCESS;}/** run resultFeb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.73 : fn_test()] : 1. psz_buf addr = (nil)Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.75 : fn_test()] : ........................................Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.78 : fn_test()] : 2. psz_buf addr = 0x1daa010Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.84 : fn_test()] : 3. psz_buf addr = 0x1daa010Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.90 : fn_test()] : 4. psz_buf addr = 0x1daa010Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.96 : fn_test()] : 5. psz_buf addr = 0x1daa010Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.102 : fn_test()] : 6. psz_buf addr = 0x1daa010Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.108 : fn_test()] : 7. psz_buf addr = 0x1daa010Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.112 : fn_test()] : ........................................Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.115 : fn_test()] : 8. psz_buf1 addr = 0x1daa010Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.121 : fn_test()] : 9. psz_buf1 addr = 0x1daa010Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.127 : fn_test()] : ........................................Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.130 : fn_test()] : 10. psz_buf addr = 0x1daa010Feb 3 21:41:09 localhost test_new[26679]: [DEBUG : main.cpp.135 : fn_test()] : 11. psz_buf1 addr = 0x1daa420*/

转载地址:https://lostspeed.blog.csdn.net/article/details/79524576 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:由map.insert使用不当引起的内存泄漏
下一篇:msdn原版纯c实现的截屏代码

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月30日 12时34分29秒