operator new[] does not take 1 parameter

Started by
10 comments, last by Toolmaker 20 years, 6 months ago
After writing my memory manager, I get that error when I compiler under release mode. This are my macro''s and functions:

// Overloaded new operator, taking as parameters the Size of the block to be

// allocated, the filename and linenumber from where it''s called

inline void * __cdecl operator new(unsigned int Size, const char *szFile, int Line)
{
    void *ptr = (void *)malloc(Size);
    if (ptr)
        CTracker::GetSingleton()->AddTrack((DWORD)ptr, Size, szFile, Line);
    return (ptr);
};

// Placement delete. In the case an exception is thrown, this version of delete is called

inline void operator delete(void *p, const char *szFile, int Line)
{

    CTracker::GetSingleton()->RemoveTrack((DWORD)p);
    free(p);
};

// Overloaded delete operator

inline void __cdecl operator delete(void *p)
{
    CTracker::GetSingleton()->RemoveTrack((DWORD)p);
    free(p);
};

// Overloaded operator new[]

inline void * __cdecl operator new[](unsigned int Size, const char *szFile, int Line)
{
    void *ptr = malloc(Size);
    if (ptr)
        CTracker::GetSingleton()->AddTrack((DWORD)ptr, Size, szFile, Line);

    return (ptr);
};

// Placement delete for operator new[]

inline void __cdecl operator delete[](void *ptr, const  char *szFile, int Line)
{
    CTracker::GetSingleton()->RemoveTrack((DWORD)ptr);
    free(ptr);
}

// Operator delete[]

inline void __cdecl operator delete[](void *ptr)
{
    CTracker::GetSingleton()->RemoveTrack((DWORD)ptr);
    free(ptr);
}


// Define the operator

#ifdef _DEBUG
    #define DEBUG_NEW new(__FILE__, __LINE__)
    #define DumpMemoryLeaks(File) { CTracker::GetSingleton()->DumpTracks(File); } 
#else
    #define DEBUG_NEW new
    #define DumpMemoryLeaks(File) { /* Empty macro */ }
#endif

#define new DEBUG_NEW
What is wrong with this? Toolmaker -Earth is 98% full. Please delete anybody you can.

Advertisement
I dont think you can use the keyword new like that, since it is a reserved keyword
Exactly what is the error your getting?



[edited by - brassfish89 on October 11, 2003 11:29:12 AM]
First of all, new is a reserved word, however, when you use new/delete in your applications, you are just calling the operator new. It's an more advanced feature.

The error I get - Note: Only happens in RELEASE mode - is as following(Or look at the thread title):

F:\Projects\CWindow\CBasicControls.h(558) : error C2660: 'new[]' : function does not take 1 parametersF:\Projects\CWindow\CBasicControls.h(686) : error C2660: 'new[]' : function does not take 1 parameters  


In case you are curious about the code that is on those lines, here we go:

char *szBuffer = new char[dwTextLength + 1];char *szText = new char[dwLength + 1];etc.  


Toolmaker


-Earth is 98% full. Please delete anybody you can.

[edited by - toolmaker on October 11, 2003 3:34:14 PM]

Overloading the global operator new and operator new[] is just asking for trouble if you arnt sure what your doing..

The problem is exactly what your compiler says it is.. you have given new and new[] 3 parameters.. and are trying to call it with only one..

Its expecting something like

.... = new(param) t[10];

but its only getting the size parameter (which should also be of type size_t). You overloaded global operators should be of the form

void* operator new(std::size_t) throw (std::bad_alloc);
void* operator new[](std::size_t) throw (std::bad_alloc);
void operator delete(void*) throw();
void operator delete[](void*) throw();

If you expect calls like
char *szBuffer = new char[dwTextLength + 1];char *szText = new char[dwLength + 1];
to work

I wouldn''t even think about making my own memory manager unless I had to.

If I had to I''d be consulting More Effective C++ the whole way, because Meyers mentions a myriad of issues that are difficult to conceive of when you are writing the manager.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
also, i'm pretty sure that the way you have currently overloaded the operators removes all the niceties of new and delete. the way it's currently written you won't call class constructors or destructors. i also get the feeling that you will no longer be creating virtual function tables (not sure if that's the right term) for any virtual functions you may have in your classes. i.e. no more polymorphism. i'm not 100% sure that the tables get created during constructor calls, but i think that is the case. basically, i think, you've turned off your ability to do OO programming.

i'm not really sure how to solve all those issues or even necessarily if they are for certain issues, but i'd suggest some heavy research into those areas before attempting to do what you're doing.

if you're just looking for memory leaks, simply wrap the new/delete operators with your leak checking code and still call the standard new/delete operators instead of just re-defining new as malloc and delete as free. this will technically not be a memory manager b/c you will still be asking the system for memory instead of grabbing it from a pre-allocated store. but since you're re-defining new as malloc you're not really doing the latter anyway, which is really what a memory manager is (it pre-allocates memory for your game and does the business of keeping your memory as unfragmented as possible).

-me

[edited by - Palidine on October 11, 2003 4:08:14 PM]
quote:won't call class constructors or destructors.
Yes it will.
quote:you will no longer be creating virtual function tables
Yes you do.

[edited by - noVum on October 11, 2003 4:10:27 PM]
Toolmaker, seems you forgot to implement new[] for the none debug call using only one parameter. Can''t find it in your code.
quote:Original post by noVum
quote:won''t call class constructors or destructors.
Yes it will.
quote:you will no longer be creating virtual function tables
Yes you do.

[edited by - noVum on October 11, 2003 4:10:27 PM]


hrm, yep...you''re right. teach me to talk before experimenting...
sorry for the erroneous comments...

-me

This topic is closed to new replies.

Advertisement