Monday, 14 July 2014

new and delete



  1.  Syntax: void*  operator new(size_t size);
      Three forms of using new
    • T* p = new T
    • T* p = new T(value)
    • T* p = new T[size]                                                                
  2. new is an operator to Allocate memory for an object or array of objects of type-name from the free store and returns a suitably typed, nonzero pointer to the object.The pointer returned is cast to the data type implicitly
  3. When new is used to allocate memory for a C++ class object, the object's constructor is called after the memory is allocated.Invokes default constructor on all the objects in case of an array of objects
  4. If unsuccessful, new returns zero or throws an exception.You can change this default behavior by writing a custom exception-handling routine and calling the  _set_new_handler run-time library function with your function name as its argument.
  5. To allocate and then frees a two-dimensional array of characters of size by 10.                                            char (*pchar)[10] = new char[dim][10];                                                                                        delete [] pchar;
  6. The type-name cannot contain constvolatile, class declarations, or enumeration declarations. Therefore, the following expression is illegal.
  7. The new operator does not allocate reference types because they are not objects.
  8. The new operator cannot be used to allocate a function, but it can be used to allocate pointers to functions. The following example allocates and then frees an array of seven pointers to functions that return integers.                                                                                                            int (**p) () = new (int (*[7]) ());                                                                                            delete *p;

No comments:

Post a Comment