overloading new and delete in c++
<p>HI All,</p>

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

<p>Currently this code is bit shabby</p>

<p>Here is my hdr file</p>

<pre><code>#include &lt;cstddef&gt;
#include &lt;iostream&gt;
#include &lt;list&gt;
#include &lt;stdarg.h&gt;
#include &lt;stdio.h&gt;
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 &gt; -1 &amp;&amp; n &lt; size)
return p;

/* failed: have to try again, alloc more mem. */
if(n &gt; -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 &lt; ALLOC_INFO* &gt; 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-&gt;address = addr;
          strncpy(info-&gt;file, fname, 63);
          info-&gt;line = lnum;
          info-&gt;size = asize;
          allocList-&gt;insert(allocList-&gt;begin(), info);
  }

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

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





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

          if(!allocList)
                  return;

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

<p>And my main.cpp is</p>

<pre><code>#include "mynew.h"
int main()
{

char *ptr = new char;
DumpUnfreed();

return 0;
}
</code></pre>

<p>When i try to compile i get the following error</p>

<pre><code>[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&amp;)
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/new:94: note:                    void* operator new(size_t, void*)
</code></pre>

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

<p>Can any one please bale me out of this </p>
