   operator new errors
  hi, i've been trying to create memory debugger, but i'm struggling to get my overloaded operator new to compile. my code is this:main.cpp#include "leaky.h"int main(){int num = new int;// Create a leakReportLeaks();// Report the leak};leaky.cpp#include "leaky.h"#undef MEM_DEBUGlist<mem_info> tracker;void * operator new(size_t size, const char *file, int line){try{// Allocate the memory check that the allocation was a success and add to the trackerif(size == 0){size = 1;// Make sure a size of 0 is not sent to malloc}void *ptr = (void *)malloc(size);if(ptr == 0){throw bad_alloc();}mem_info m_info(ptr, size, file, line);AddTracker(m_info);return(ptr);}catch(bad_alloc){// Handle an errorfstream mem("leaks.txt");mem << "MEMORY ALLOCATION ERROR" << "\n";}};void operator delete(void *p){RemoveTracker((void *)p);free(p);};void AddTracker(mem_info minfo){tracker.push_back(minfo);};void RemoveTracker(void *ptr){list<mem_info>::iterator iter;for(iter = tracker.begin(); iter != tracker.end(); iter++){if((*iter).GetPointer() == ptr){tracker.remove((*iter));break;}}};void ReportLeaks(){list<mem_info>::iterator iter;fstream mem("leaks.txt");// Report the memory leaksmem << "\tAddress\t" << "\tSize\t" << "\tFile\t" << "\tLine\t" << "\n\n";mem << "\t----------" << "----------" << "----------" << "----------" << "\n\n";for(iter = tracker.begin(); iter != tracker.end(); iter++){mem << "\t" << (*iter).GetPointer() << "\t\t" << (*iter).GetSize() << "\t\t"<< (*iter).GetFile() << "\t\t" << (*iter).GetLine() << "\t\n\n";}for(iter = tracker.begin(); iter != tracker.end(); iter++){free((*iter).GetPointer());// Make sure that no memory is left allocated}mem.close();};// return file nameconst char* mem_info::GetFile(){return file;};// return line numberint mem_info::GetLine(){return line;};// return address of objectvoid * mem_info::GetPointer(){return ptr;};// return size of objectsize_t mem_info::GetSize(){return size;};bool mem_info::operator ==(const mem_info &other) const{return this->ptr == other.ptr;}#define MEM_DEBUGleaky.h///////////////////////////////////////////////////////////////////////Detect memory leaks   ///////////////////////////////////////////////////////////////////////#include <cstddef>// For size_t#include <list>// For list#include <stdlib.h>// For malloc and free#include <stdexcept>// For bad_alloc#include <fstream>// For file accessusing std::size_t;using std::bad_alloc;using std::list;using std::fstream;// replace the default new with new modified version#ifdef MEM_DEBUG#define new new(__FILE__, __LINE__)#endif#undef MEM_DEBUGvoid* operator new(size_t size, const char *file, int line);void operator delete(void *p);class mem_info{public:mem_info(void *mptr, size_t msize, const char* mfile, int mline) : ptr(mptr), size(msize), file(mfile), line(mline) {};// Set up the class~mem_info() {};// Cleanup the classvoid * GetPointer();// Get the address of the objectsize_t GetSize();// Get the size of the objectconst char* GetFile();// Get the file nameint GetLine();// Get the line numberbool operator==( const mem_info &other ) const;private:void *ptr;// The address of the allocated memorysize_t size;// The size on the memory allocatedconst char* file;// The file in which the allocation accuredint line;// The line in which the allocation accured};void AddTracker(mem_info minfo);// Add the allocated memory info to the listvoid RemoveTracker(void *ptr);// Remove the allocated memory info from the listvoid ReportLeaks();// Report the leaks#define MEM_DEBUGand the errors are:------ Build started: Project: leaky, Configuration: Debug Win32 ------Compiling...leaky.cppc:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2365: 'operator new' : redefinition; previous definition was 'function'c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2078: too many initializersc:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'   Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style castc:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2143: syntax error : missing ';' before '('c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2226: syntax error : unexpected type 'size_t'c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2059: syntax error : ')'c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2365: 'operator new' : redefinition; previous definition was 'function'c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2078: too many initializersc:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'   Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style castc:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2143: syntax error : missing ';' before '('c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2226: syntax error : unexpected type 'size_t'c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2059: syntax error : ')'c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(7) : error C2143: syntax error : missing ';' before '{'c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(7) : error C2447: '{' : missing function header (old-style formal list?)main.cppc:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2365: 'operator new' : redefinition; previous definition was 'function'c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2078: too many initializersc:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'   Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style castc:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2143: syntax error : missing ';' before '('c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2226: syntax error : unexpected type 'size_t'c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2059: syntax error : ')'c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\main.cpp(5) : error C2661: 'operator new' : no overloaded function takes 3 argumentsGenerating Code...Build Time 0:01Build log was saved at "file://c:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\leaky\leaky\Debug\BuildLog.htm"leaky - 21 error(s), 0 warning(s)========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========btw i'mn using mscv 2005 and am defining MEM_DEBUG in project settingsthanks jason   
  I *think* i had similar problems to this and i believe mine were down to the #defining of file and line numbers into the new call.writing a new that follows one of the regular ones first, without file and line positions.Dave   ;  oops wrong secxtion could a mod please move to general   ;  Your error is in the following piece of code (well one of them is anyway, don't know if there is more).#ifdef MEM_DEBUG#define new new(__FILE__, __LINE__)#endif#undef MEM_DEBUGvoid* operator new(size_t size, const char *file, int line);Now imagine if MEM_DEBUG is defined when this code is read. The preprocessor will transform it into this:void* operator new(__FILE__,__LINE__)(size_t size, const char *file, int line);Which gives you an error. Undefining MEM_DEBUG won't help since you have already defined new, and undefining MEM_DEBUG won't change that.   ;  thanks i've got that working but i need to overload new[] and delete[], would something like this write?operator new[x](size_t size, const char* file, int line){   for( int y = 0; y < x; y++ )   { malloc(size);   }};if how does it work?thanks jason   ;  Go to Fluid Studios website and download their Memory Manager (on main page near the bottom).  Everything you want is already written for you.  If you're doing it to learn looking over their code would likely help you too.   