new & delete operators
Properties of 'new'
- 'new' operator initializes new object. i.e; it first calls the memmory allocation premitive, then call the appropriate constructor of the class.
- It is type safe.
- The 'new' operator can be overloaded by a class.
- 'new' operator never ever returns NULL. Instead, if 'new' operator runs out of memory during new operation, it throws an exception of type bad_alloc. (C's malloc() returns NULL)
- set_new_handler() installs the new handler.
- The placement syntax of the new operator, construct the object at a predetermined position in memory (void* place = (void*) 0xFEEDBABE; Fred* p = new(place) Fred(42, 42);).We should explicitly call the object's destructor after placement syntax.
- If an exception occurs in the constructor during p = new SomeClass(), the sizeof(SomeClass) bytes that were allocated are automatically released back to the heap.
Properties of 'delete'
- 'delete' is a two-step operation: it first calls the destructor on the object pointer, say p, then it releases the memory pointed to by p using a memory deallocation primitive.
- Calling delete p when p is NULL is safe and is guaranteed to do nothing.
- Never delete call a pointer twice, Is a DISASTER!. This is likely to corrupt the heap and its list of free memory.
- delete[] p is for deleting an array of things.
- Its legal for a member function to say delete this, but be careful.
- The realloc move data to a new location, it uses a bitwise copy
Difference between 'operator new' and the 'new' operator?
- The term "operator new" is used incase when u are overloading the global "new" operator.
- The term "new operator" is used in case of dynamic allocation of memory.When a variable is allocated memory dynamically the new operator is used.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home