RAD Studio
ContentsIndex
PreviousUpNext
The new And delete Operators

This section contains new And delete Operator topics.

Name 
Description 
By default, new throws the bad_alloc exception when a request for memory allocation cannot be satisfied.
You can define a function to be called if the new operator fails. To tell the new operator about the new-handler function, use set_new_handler and supply a pointer to the new-handler. If you want new to return null on failure, you must use set_new_handler(0) . 
The placement syntax for operator new( ) can be used only if you have overloaded the allocation operator with the appropriate arguments. You can use the placement syntax when you want to use and reuse a memory space which you set up once at the beginning of your program.
When you use the overloaded operator new( ) to specify where you want an allocation to be placed, you are responsible for deleting the allocation. Because you call your version of the allocation operator, you cannot depend on the global ::operator delete( ) to do the cleanup.
To release memory, you... more 
The global operators, ::operator delete(), and ::operator delete[]() cannot be overloaded. However, you can override the default version of each of these operators with your own implementation. Only one instance of the each global delete function can exist in the program.
The user-defined operator delete must have a void return type and void* as its first argument; a second argument of type size_t is optional. A class T can define at most one version of each of T::operator delete[]() and T::operator delete(). To overload the delete operators, use the following prototypes.
  • void operator delete(void *Type_ptr, [size_t... more 
The global ::operator new() and ::operator new[]() can be overloaded. Each overloaded instance must have a unique signature. Therefore, multiple instances of a global allocation operator can coexist in a single program.
Class-specific memory allocation operators can also be overloaded. The operator new can be implemented to provide alternative free storage (heap) memory-management routines, or implemented to accept additional arguments. A user-defined operator new must return a void* and must have a size_t as its first argument. To overload the new operators, use the following prototypes declared in the new.h header file.
  • void * operator new(size_t Type_size); // For... more 
Arrays are deleted by operator delete[](). You must use the syntax delete [] expr when deleting an array.  
By default, if there is no overloaded version of new, a request for dynamic memory allocation always uses the global version of new, ::operator new(). A request for array allocation calls ::operator new[](). With class objects of type name, a specific operator called name::operator new() or name::operator new[]() can be defined. When new is applied to class name objects it invokes the appropriate name::operator new if it is present; otherwise, the global ::operator new is used.
Only the operator new() function will accept an optional initializer. The array allocator version, operator new[](), will not accept... more 
When using the array form of operator new[](), the pointer returned points to the first element of the array. When creating multidimensional arrays with new, all array sizes must be supplied (although the leftmost dimension doesn't have to be a compile-time constant):  
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!