Wednesday, January 6, 2010

new & delete in C/C++

new :
void* operator new (std::size_t size) throw (std::bad_alloc);
void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw();
Allocate storage space

The first version allocates size bytes of storage space, aligned to represent an object of that size, and returns a non-null pointer to the first byte of this block. On failure, it throws a bad_alloc exception.

The second version is the nothrow version. It does the same as the first version, except that on failure it returns a null pointer instead of throwing an exception.

operator new are declared in the global namespace, not in the std namespace.
The first and second versions are implicitly declared in every translation unit of a C++ program: The header does not need to be included for them to be present.

operator new can be called explicitly as a regular function, but in C++, new is an operator with a very specific behavior: An expression with the new operator, first calls function operator new with the size of its type specifier as first argument, and if this is successful, it then automatically initializes or constructs the object (if needed). Finally, the expression evaluates as a pointer to the appropriate type.


delete :
void operator delete (void* ptr) throw ();
void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) throw();
Deallocate storage space

The first and second versions deallocate the memory block pointed by ptr (if not-null), releasing the storage space previously allocated to it by a call to operator new and making that pointer location invalid.

The second and third versions cannot be implicitly called by the operator expression (the delete operator calls once the function operator delete for each of its arguments). Although they can be called explicitly as operator new function calls, their default definitions serve no particular purpose - they are provided as counterparts for the operator new functions and called accordingly when done automatically.

operator delete can be called explicitly as a regular function, but in C++, delete is an operator with a very specific behavior: An expression with the delete operator, first calls the appropriate destructor (if needed), and then calls function operator delete to release the storage.

// operator delete example
#include < iostream >
#include < new >
using namespace std;

struct myclass {
myclass() {cout <<"myclass constructed\n";} ~myclass() {cout <<"myclass destroyed\n";} }; int main () { myclass * pt; pt = new myclass; delete pt; return 0; } Output: myclass constructed myclass destroyed Example of a new: #include < iostream >
#include < new >
using namespace std;

struct myclass {myclass() {cout <<"myclass constructed\n";}};

int main () {

int * p1 = new int;
// same as:
// int * p1 = (int*) operator new (sizeof(int));

int * p2 = new (nothrow) int;
// same as:
// int * p2 = (int*) operator new (sizeof(int),nothrow);

myclass * p3 = (myclass*) operator new (sizeof(myclass));
// (!) not the same as:
// myclass * p3 = new myclass;
// (constructor not called by function call, even for non-POD types)

new (p3) myclass; // calls constructor
// same as:
// operator new (sizeof(myclass),p3)

return 0;
}

Output: myclass constructed

No comments:

Post a Comment