Quick question here:
Is it actually possible to override the new/delete operators with your own, custom ones? I wanted to experiment a little, but I wanted the same syntax as the new/delete.
I don't know if it's possible, though?
Printable View
Quick question here:
Is it actually possible to override the new/delete operators with your own, custom ones? I wanted to experiment a little, but I wanted the same syntax as the new/delete.
I don't know if it's possible, though?
You can overload the new and delete operators for your class.
Ah, but not global ones, eh?
>Is it actually possible to override the new/delete operators with your own, custom ones?
Yes, it is. Do you want to replace the global new and delete or add custom functionality to one of your classes?
I wanted to override the global ones, in so that I could use my own allocation system.
But I don't know quite how to do it.
Just adding a new operator new at global level will, of course, gives function redefinition error.
Something like...
You do this in the global namespace.Code:#include <cstdlib> // for malloc
#include <exception> // bad_alloc probably
#include <new>
void* operator new (size_t size) { /*... malloc(), throw, et cetera */ }
void operator delete (void *p) { /* ... free(), et cetera ... */ }
>Just adding a new operator new at global level will,
>of course, gives function redefinition error.
Post your code. Also, be sure to match the declaration exactly:
Code:#include <cstddef>
#include <iostream>
#include <exception>
#include <new>
void *operator new ( std::size_t size )
throw ( std::bad_alloc )
{
std::cout<<"new\n";
return 0;
}
void operator delete ( void *p )
throw()
{
std::cout<<"delete\n";
}
int main()
{
int *x = new int;
delete x;
}
Just doing this
Gives a bunch of errors, the most critical one being:Code:void *operator new ( std::size_t size )
throw ( std::bad_alloc )
{
std::cout<<"new\n";
return 0;
}
void operator delete ( void *p )
throw()
{
std::cout<<"delete\n";
}
The rest are related to that one.Code:error C2365: 'operator new' : redefinition; previous definition was 'function'
Btw, Visual Studio doesn't support any types in throw, just ... or empty. But it doesn't seem to come to that.
I'm going to try a fresh project first and see if that helps.
EDIT: Yep, it's probably a header clash. It works fine in a fresh project. Hmmm. Is it MFC?
EDIT2: Yup, that's the culprit. MFC is conflicting with overriding the new/delete operators somehow.
Are you by any chance using a library that already redefines new and delete?
MFC does, apparently.
yup. Just saw your edits
Hmm. So how to work around this? It it possible to hide or is the way just to rename it to something else?
EDIT: Made some progress now. Now I just get multiple symbols error. Not sure how to fix that yet, however.
EDIT2: Actually, after just another compile, it compiled fine, so I successfully override(ed?) them. Now let's see see if I can override the debug new too.
I did it!
For anyone who is interested in how, it seems the new macro was getting in the way (as you know, MFC defaults to defining new as DEBUG_NEW in debug builds), so I had to push the macro, define my new, and then pop it.
At first I got multiple symbols errors, but when compiling again, they disappeared and when running the code, the compiler called the correct function!
The only problem is that I can't call the original operator new >_<Code:#pragma push_macro("new")
#undef new
void* __cdecl operator new (std::size_t size)
{
std::cout<<"new\n";
return 0;
}
void* __cdecl operator new(std::size_t size, LPCSTR strLine, int nLine)
{
std::cout<<"new\n";
return 0;
}
void operator delete (void* p)
{
std::cout<<"delete\n";
}
#pragma pop_macro("new")
But I might be able to call placement new, which I'm not very familiar with the syntax...