View Full Version : overloading new and delete
liquience
12-07-2002, 03:27 PM
Right now I'm curious about if there is a method to overload new and delete without using malloc() and free(), in the proper places of course. One of my friends argues that in C++ there is no need to use C functions (meaning malloc and free) in a C++ program and that there is "a simpler way."
Is he right? Are there any other functions that would let you overload new and delete (as well as their subscripted counterparts) without using the standard malloc and free that everyone is familiar with? Is his point even valid? I'm not sure that he's right at all, but I figured I'd ask.
thanks much!
sans-hubris
12-08-2002, 03:45 AM
That's what class constructors and destructors do, overload new and delete, respectively. And yes, there really is no reason to be using malloc() and free() in C++. new and delete, when used, will always call the constructor <edit>or destructor, respectively,</edit> for a class.
bwkaz
12-08-2002, 09:36 AM
If, for some reason, you think you need to overload new and delete manually, and if, in the new "operator new" function, you need to allocate memory, just use new, like you normally would.
phubuh
12-08-2002, 07:38 PM
new is basically the same thing as malloc, except that the former can allocate memory for objects; the same thing goes for delete vs. free. Anything you can do with the C variants, you can do with the C++ ones. So no, there is absolutely no excuse for using malloc and free in a C++ program.
CaptainCode
07-22-2003, 10:05 AM
You can use malloc, but only in very rare applications where you are trying to squeeze as much performance as possible out of your program. You also have to know what you are doing when using them otherwise it won't make a difference in speed and may even be slower. For the most part, it's not needed.
DNAunion2000
07-23-2003, 09:15 PM
DNAunion: I seemed to remember one of my C++ books using malloc when it redefined new, but I cant find that right now. But I did find the following on the web.
How to overload NEW and DELETE operators - by Borland Developer Support Staff
Technical Notes Database
TN3041C.txt How to overload NEW and DELETE operators
Category :C++
Platform :All
Product :C++ All
Description:
Overloading NEW and DELETE
==========================
If the functionality of 'new' and 'delete' are unsatisfactory for your
needs, you can change their meanings. This is usually accomplished by
overloading them on a class-by-class basis. When you overload 'new' and
'delete' you must use the keyword OPERATOR before 'new' and 'delete'.
For example:
void * operator new(int i)
{
...
};
and
void operator delete(void *)
{
...
};
Here's a working example, which replaces 'new' and 'delete' with
malloc() and free():
#include
#include
// Global
void * operator new(size_t size)
{
puts("global new\n");
return malloc(size);
};
void operator delete(void * mem)
{
puts("global delete\n");
free(mem);
};
(http://bdn.borland.com/article/0,1410,18041,00.html)
inline void * __cdecl 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 __cdecl operator delete(void *p)
{
RemoveTrack((DWORD)p);
free(p);
};
(http://www.flipcode.com/tutorials/tut_memleak.shtml)
_set_new_handler
Transfers control to your error-handling mechanism if the new operator fails to allocate memory.
Remarks
The C++ _set_new_handler function specifies an exception-handling function that gains control if the new operator fails to allocate memory. If new fails, the run-time system automatically calls the exception-handling function that was passed as an argument to _set_new_handler.
The C++ _set_new_mode function sets the new handler mode for malloc. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when malloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call
_set_new_mode(1)
early in your program or link with NEWMODE.OBJ.
If a user-defined operator new is provided, the new handler functions are not automatically called on failure. (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_CRT__set_new_handler.asp)
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.