Archived

This topic is now archived and is closed to further replies.

operator new[] does not take 1 parameter

This topic is 5177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 parameters
F:\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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Looking at the preprocessor directives at the bottom of your posted code, you only want to use the overloaded versions in DEBUG mode? In that case, try putting #ifdef _DEBUG around the declarations/definitions of the overloaded operators.

Share this post


Link to post
Share on other sites
Well, I solved my problem. I renamed the thing to MemoryTracer, happy now? (Ok, stupid joke).

And, since I only want to trace any memory allocations done in debug mode, I placed everything between #ifdef _DEBUG ... #else #endif code. Problem solved.

Toolmaker



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

Share this post


Link to post
Share on other sites