当前位置: 首页 > news >正文

记录一个标记所有new出来的内存的地址加上TAG

#pragma once #include <iostream> #include <cstring> #include <cstdlib> #include <new> #include <typeinfo> // 可选,如果不想用宏自动传名可以结合 typeid // 只在调试模式下启用 #ifdef _DEBUG struct MemHeader { char className[128]; // 存储类名 size_t size; // 用户请求的字节数 // 可以添加:file, line, timestamp 等 }; // 带类名参数的 operator new inline void* operator new(size_t sz, const char* name) { size_t total = sizeof(MemHeader) + sz; void* raw = malloc(total); if (!raw) throw std::bad_alloc(); MemHeader* h = static_cast<MemHeader*>(raw); // 安全复制类名,截断超过 127 个字符 strncpy(h->className, name, sizeof(h->className) - 1); h->className[sizeof(h->className) - 1] = '\0'; h->size = sz; // 返回用户区域指针(头部之后) return static_cast<char*>(raw) + sizeof(MemHeader); } // 对应的 operator delete(释放时回退头部) inline void operator delete(void* p) noexcept { if (!p) return; void* raw = static_cast<char*>(p) - sizeof(MemHeader); free(raw); } // 为了安全,也重载 placement delete(如果构造函数抛出异常会调用) inline void operator delete(void* p, const char*) noexcept { ::operator delete(p); } // --- 宏定义(自动传入类名)--- // 用法:NEW(MyClass) → 调用默认构造 // NEW(MyClass, arg1) → 调用带参构造 #define NEW(T, ...) new (#T) T(__VA_ARGS__) #else // 非调试模式,直接使用标准 new #define NEW(T, ...) new T(__VA_ARGS__) #endif

main.cpp

#include "debug_new.h" #include <iostream> #include <unistd.h> class MyClass { public: MyClass() { std::cout << "MyClass default constructed" << std::endl; } MyClass(int x) { std::cout << "MyClass constructed with " << x << std::endl; } ~MyClass() { std::cout << "MyClass destroyed" << std::endl; } }; class Another { int a; public: Another(int a, double b) : a(a) { std::cout << "Another(" << a << ", " << b << ")" << std::endl; } }; int main() { // 使用 NEW 宏,自动传入类名 MyClass* obj1 = NEW(MyClass); MyClass* obj2 = NEW(MyClass, 42); Another* obj3 = NEW(Another, 10, 3.14); while(1) sleep(1); delete obj1; delete obj2; delete obj3; return 0; }

armg++ -D_DEBUG -g -o myapp main.cpp -std=c++11

Breakpoint 2, MyClass::MyClass (this=0x424ca8) at main.cpp:7 7 in main.cpp (gdb) bt #0 MyClass::MyClass (this=0x424ca8) at main.cpp:7 #1 0x0000000000400e4c in main () at main.cpp:20

dump

(gdb) p (char*)0x424ca8 - 136 $2 = 0x424c20 "MyClass" (gdb) dump memory /data/pub/d.bin 0x424ca8 0x445000 (gdb) dump memory /data/pub/d.bin 0x424ca8-136 0x445000
root@hovfree:/data/pub# hexdump -C d.bin 00000000 4d 79 43 6c 61 73 73 00 00 00 00 00 00 00 00 00 |MyClass.........| 00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| *
http://www.jsqmd.com/news/1106121/

相关文章:

  • AI 辅助:Product Hunt 发布复盘:上线当天之前,准备已经开始
  • Cursor Free VIP破解工具:3分钟解除AI编程助手试用限制的终极指南
  • 西安共享茶室平台开发?时段预约锁房技术源码讲解
  • 封装统一多模态客户端(整合文字对话 + 文生图 + 语音转写)
  • 利用金字塔原理学习PHP的具象化的庖丁解牛
  • 汽车电子散热管理:DRV8213驱动器与MF25060V2风扇实战
  • 【小白也能轻松玩转龙虾】虾壳云一键部署入门攻略,分步搭建桌面端 OpenClaw v2.7.9(附最新安装包)
  • React 渲染性能:组件边界、状态下沉与重渲染治理
  • 后端开发者转型AI大模型的必备技能与实战指南
  • AI 辅助:独立开发者技术选型:最好的技术是能让产品活下去
  • AI 辅助:少说漂亮话:基础设施要用事故假设来设计
  • AI 辅助:独立创作:工具应放大作者,而不是替代作者
  • 一文看懂 DDoS 与 CC 攻击:攻击类型全解析 + 完整防护方案
  • 5个场景化解决方案:用taskt告别重复劳动,实现桌面自动化革命
  • Harness Engineering(驾驭工程)简单的演化过程
  • 阿贝云免费云服务器磁盘空间合理分配实操心得
  • 2025了会议纪要还写得慢又漏任务?听脑帮你智能提取任务超省心!
  • 云原生 AI 平台搭建:先把模型服务当普通服务治理
  • 一张图讲清楚:MCP边界
  • “借道”MoP封装,AMD打破“存储墙”与“空间锁”
  • 2.4 中间层:底层驱动与标准库——固收与负债的“稳态输出”
  • 那些与量子纠缠有关的物理概念和现象
  • QKeyMapper:Windows平台专业级全能按键映射引擎架构解析
  • 子任务想换个便宜模型跑?Sub-Agent 这样设计
  • 语音一键转文字超简单!2026多款免费软件详细步骤,新手一看就会
  • 小学算术题
  • 用最新 GPT-5.6 润色论文是一种怎么样的体验?
  • 开源视频生成模型选择
  • SpringBoot+Vue 私人西服定制_leabo管理平台源码【适合毕设/课设/学习】Java+MySQL
  • 检索增强架构实践:家庭回忆录助手如何避免编造