重载新的和删除在c ++ [英] overloading new and delete in c++

查看:304
本文介绍了重载新的和删除在c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI All,



我试图重载新的和删除,以修复我的项目中的内存泄漏问题。



这里是我的hdr文件



  #include< cstddef> 
#include< iostream>
#include< list>
#include< stdarg.h>
#include< stdio.h>
using namespace std;
typedef unsigned int DWORD;
void AddTrack(DWORD addr,DWORD asize,const char * fname,DWORD lnum);
char * OutputDebugString(const char * fmt,...);
void RemoveTrack(DWORD addr);
void DumpUnfreed();

#ifdef _DEBUG
#define DEBUG_NEW new(__ FILE__,__LINE__)
#define new DEBUG_NEW


void * operator new(unsigned int size,const char * file,int line)
{
void * ptr =(void *)malloc
AddTrack((DWORD)ptr,size,file,line);
return(ptr);
}
/ * inline void * operator new(unsigned int size)
{
void * ptr =(void *)malloc
AddTrack((DWORD)ptr,size,_FILE _,_LINE_);
return(ptr);
} * /
void operator delete(void * p)
{
RemoveTrack((DWORD)p);
free(p);
}

#endif


char * OutputDebugString(const char * fmt,...)
{

char * p = NULL;
size_t size = 1024;
int n = 0;
va_list ap;

if((p =(char *)malloc(size))== NULL)
return NULL;

while(1){
va_start(ap,fmt);
n = vsnprintf(p,size,fmt,ap);
va_end(ap);

if(n> -1&& n< size)
return p;

/ *失败:必须重试,分配更多mem。 * /
if(n> -1)/ * glibc 2.1 * /
size = n + 1;
else / * glibc 2.0 * /
size * = 2; / *两倍的旧大小* /

if((p =(char *)realloc(p,size))== NULL)
return NULL;
}
}

typedef结构信息{
DWORD地址;
DWORD size;
char file [64];
DWORD行;
} ALLOC_INFO;

typedef list< ALLOC_INFO *> AllocList;
AllocList * allocList;




void AddTrack(DWORD addr,DWORD asize,const char * fname,DWORD lnum)
{
ALLOC_INFO * info;

if(!allocList){
// allocList = new AllocList;
allocList =(AllocList *)malloc(sizeof(AllocList));
}

// info = new(ALLOC_INFO);
info =(ALLOC_INFO *)malloc(sizeof(ALLOC_INFO));
info->地址= addr;
strncpy(info-> file,fname,63);
info-> line = lnum;
info-> size = asize;
allocList-> insert(allocList-> begin(),info);
}

void removeTrack(DWORD addr)
{
AllocList :: iterator i;

if(!allocList)
if(!allocList)
return;
for(i = allocList-> begin(); i!= allocList-> end(); i ++)
{
if((* i) - & addr)
{
allocList-> remove((* i));
break;
}
}
}





void DumpUnfreed()
{
AllocList :: iterator i;
DWORD totalSize = 0;
char buf [1024];

if(!allocList)
return;

for(i = allocList-> begin(); i!= allocList-> end(); i ++){
sprintf(buf,%-50s:\t \tLINE%d,\t\tADDRESS%d\t%d unfreed\\\

(* i) - > file,(* i) - >行, ) - >地址,(* i) - > size);
OutputDebugString(%s,buf);
totalSize + =(* i) - > size;
}
sprintf(buf,------------------------------------ ----------------------- \\\
);
OutputDebugString(%s,buf);
sprintf(buf,Total Unfreed:%d bytes\\\
,totalSize);
OutputDebugString(%s,buf);
}

我的main.cpp是

  #includemynew.h
int main()
{

char * ptr = new char;
DumpUnfreed();

return 0;
}

当我尝试编译时,我得到以下错误

  [root @ dhcppc0 new]#!g 
g ++ main.cpp -D_DEBUG
mynew.h:25:error:declaration 'new new'as non-function
main.cpp:在函数'int main()':
main.cpp:9:error:没有匹配的函数调用'operator new(unsigned int ,const char [9],int)'
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1 / new:84:note:candidate are:void * operator new(size_t)
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../ include / c ++ / 4.1.1 / new:88:note:void * operator new(size_t,const std :: nothrow_t&)
/usr/lib/gcc/i386-redhat-linux/4.1.1/。 ./../../../include/c++/4.1.1/new:94:note:void * operator new(size_t,void *)

我知道我的#defines有一些问题,但我不知道是什么问题。



$ c>宏之前你的函数。您的代码最终如下所示:

  void * 
运算符new(__ FILE__,__LINE __) char * file,int line)

这显然是错误的。你应该移动函数下面的宏定义(或者更好的是将这些函数保存在你链接的.cpp文件中)。对于值得的, new



我最近发布了我的全局内存操作符框架。它可能会帮助你一点。


HI All,

I was trying to overload new and delete to fix a memory leak problem in my project. But got stuck with some compilation error.

Currently this code is bit shabby

Here is my hdr file

#include <cstddef>
#include <iostream>
#include <list>
#include <stdarg.h>
#include <stdio.h>
using namespace std;
typedef unsigned int  DWORD;
void AddTrack(DWORD addr,  DWORD asize,  const char *fname, DWORD lnum);
char *OutputDebugString (const char *fmt, ...);
void RemoveTrack(DWORD addr);
void DumpUnfreed();

#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW


   void *  operator new (unsigned int size, const char *file, int line)
  {
          void *ptr = (void *)malloc(size);
          AddTrack((DWORD)ptr, size, file, line);
          return(ptr);
  }
  /*inline void * operator new(unsigned int size)
  {
          void *ptr = (void *)malloc(size);
          AddTrack((DWORD)ptr, size, _FILE_,_LINE_);
          return(ptr);
  }*/
  void  operator delete(void *p)
  {
          RemoveTrack((DWORD)p);
          free(p);
  }

#endif


char *OutputDebugString (const char *fmt, ...)
{

char *p = NULL;
 size_t size = 1024;
 int n = 0;
 va_list ap;

 if((p = (char*) malloc(size)) == NULL)
 return NULL;

 while(1) {
  va_start(ap, fmt);
  n = vsnprintf(p, size, fmt, ap);
  va_end(ap);

 if(n > -1 && n < size)
return p;

/* failed: have to try again, alloc more mem. */
if(n > -1)      /* glibc 2.1 */
size = n + 1;
else            /* glibc 2.0 */
size *= 2;     /* twice the old size */

if((p = (char *)realloc (p, size)) == NULL)
return NULL;
}
}

typedef struct information {
DWORD   address;
DWORD   size;
char    file[64];
DWORD   line;
} ALLOC_INFO;

typedef list < ALLOC_INFO* > AllocList;
AllocList *allocList;




  void AddTrack(DWORD addr,  DWORD asize,  const char *fname, DWORD lnum)
  {
          ALLOC_INFO *info;

          if(!allocList) {
          //allocList = new AllocList;
            allocList = (AllocList*)malloc (sizeof (AllocList));
          }

          //info = new(ALLOC_INFO);
          info = (ALLOC_INFO*) malloc (sizeof (ALLOC_INFO));
          info->address = addr;
          strncpy(info->file, fname, 63);
          info->line = lnum;
          info->size = asize;
          allocList->insert(allocList->begin(), info);
  }

  void RemoveTrack(DWORD addr)
  {
          AllocList::iterator i;

          if(!allocList)
          if(!allocList)
                  return;
          for(i = allocList->begin(); i != allocList->end(); i++)
          {
                  if((*i)->address == addr)
                  {
                          allocList->remove((*i));
                          break;
                  }
          }
  }





void DumpUnfreed()
  {
          AllocList::iterator i;
          DWORD totalSize = 0;
          char buf[1024];

          if(!allocList)
                  return;

          for(i = allocList->begin(); i != allocList->end(); i++) {
                  sprintf(buf, "%-50s:\t\tLINE %d,\t\tADDRESS %d\t%d unfreed\n",
                          (*i)->file, (*i)->line, (*i)->address, (*i)->size);
                  OutputDebugString("%s",buf);
                  totalSize += (*i)->size;
          }
          sprintf(buf, "-----------------------------------------------------------\n");
          OutputDebugString("%s",buf);
          sprintf(buf, "Total Unfreed: %d bytes\n", totalSize);
          OutputDebugString("%s",buf);
  }

And my main.cpp is

#include "mynew.h"
int main()
{

char *ptr = new char;
DumpUnfreed();

return 0;
}

When i try to compile i get the following error

[root@dhcppc0 new]# !g
g++ main.cpp -D_DEBUG
mynew.h:25: error: declaration of ‘operator new’ as non-function
main.cpp: In function ‘int main()’:
main.cpp:9: error: no matching function for call to ‘operator new(unsigned int, const     char [9], int)’
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/new:84: note:   candidates are: void* operator new(size_t)
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/new:88: note:                     void* operator new(size_t, const std::nothrow_t&)
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/new:94: note:                    void* operator new(size_t, void*)

I know there is some thing wrong with my #defines, but I am not sure what is wrong.

Can any one please bale me out of this

解决方案

You've defined your new macro before your functions. Your code ends up looking like:

void *
  operator new(__FILE__, __LINE__)(unsigned int size, const char *file, int line)

Which is obviously wrong. Your should move the macro definitions underneath the functions (or better is to keep those functions in a .cpp file you link with.) For what it's worth, new is a keyword and cannot be an identifier, so your program is, strictly speaking, ill-formed.

I recently posted my global memory operators framework. It might help you a bit.

这篇关于重载新的和删除在c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆