重载new delete操作符打印堆上内存分配信息

本文介绍了如何通过重载new和delete操作符来打印内存分配的详细信息,以帮助检测内存泄露。当尝试重载delete时,可能会导致递归删除的问题,而使用malloc和free则不会出现此问题。文章提供了多个参考资料链接,进一步探讨这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



重载new操作符,打印new分配堆上的内存时的地址,文件,所在行数,以便监测内存泄露

#include <iostream>
#include <string>
#include <vector>
#include <cstddef>
#define DEBUG
using namespace std;

#ifdef DEBUG
void *operator new(size_t size, const char *file, int line)
{
 void *ptr = ::operator new(size);
cout << &ptr <<" " << file << " " << line << endl;
return ptr;
}
#define new new(__FILE__, __LINE__)
#endif

int main(int argc, const char *argv[])
{
   int *p = new int(6);   ---》只有当前文件时,不用定义宏,直接int *p = new(__FILE__,__LINE__)int;
   cout << *p << endl;
   return 0;
}

运行结果:

[syswj@host mianshi]$ ./a.out
0xbfc282ec new_delete.cpp 21
0

问题:重载delete时调用全局的operator delete 会一直递归删除下去,

若是把全局的operator new 和operator delete 改成malloc 和free则没问题

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <cstddef>
#define DEBUG
using namespace std;

#ifdef DEBUG
void *operator new(size_t size, const char *file,int line)
{
    void *ptr = ::operator new(size);
    cout <<"new:" << &ptr <<" "<< size <<" " << file << " " << line << endl;
    return ptr;
}

void *operator new[](size_t size, const char *file, int line)
{
    return operator new(size, file, line);
}

void operator delete(void *ptr, const char *file, int line)
{
    cout << "delete " <<&ptr <<" " << file << " " << line << endl;
    ::operator delete (ptr) ;
}

void operator delete[](void *ptr, const char *file, int line)//new产生堆上的数组时,会在前一个字节记录大小
{
    int *p = (int*)ptr - 1;
    cout <<*p << endl;
    operator delete(ptr, file, line);
}
#define new new(__FILE__, __LINE__)
#define delete delete(__FILE__, __LINE__)
#endif

int main(int argc, const char *argv[])
{
    string *p = new string("hello how");
    cout << *p << endl;
    int *a = new int[23];

    //    delete p;
    //  delete []a;
    return 0;
}

delete不能这重载,有问题!!!

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <cstddef>
#include <typeinfo>
#define DEBUG
using namespace std;

#ifdef DEBUG
void *operator new(size_t size, const char *file,int line)
{
    void *ptr = malloc(size);
    cout <<"new:" << ptr <<" "<< size <<" " << file << " " << line << endl;
    return ptr;
}

void *operator new[](size_t size, const char *file, int line)
{
    return operator new(size, file, line);
}

void operator delete(void *ptr)
{
    cout << "delete " <<ptr  << endl;
    free(ptr) ;
}

void operator delete[](void *ptr)//new产生堆上的数组时,会在前一个字节记录大小
{
    cout << "[]" << endl;
    int *p = (int*)ptr - 1;
    cout << dec <<*p <<endl;
    operator delete(ptr);
}
#define new new(__FILE__, __LINE__)
#endif

int main(int argc, const char *argv[])
{
    string *p = new string("hello how");
    cout << *p << endl;
    int *a = new int[23];
    cout << "string " << hex << p << endl ;
    cout << "a[] " << hex << a << endl; 

    delete p;
    delete []a;
    return 0;
}


http://blog.youkuaiyun.com/hopeztm/article/details/7974643 

http://blog.youkuaiyun.com/tedious/article/details/6789926 

http://blog.sina.com.cn/s/blog_3f56d7800100i1uf.html 

http://c.chinaitlab.com/example/958786_2.html 

http://www.cnblogs.com/eahom/p/3615900.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值