<<< More function default parameters     Index     Destructors >>>

11. Dynamic memory allocation

  • 
    #include "point.h"
    
    int main()
    {
        int* ptr_2_int = new int;
        int* ptr_2_array = new int[ 100 ];
    
        int size = 1000;
        Point* ptr_points = new Point[ size ];
    
        //...
        delete ptr_2_int;
        delete[] ptr_2_array;
        delete[] ptr_points;
        return 0;
    }
    
    
  • new and delete are keywords

  • Remember to use delete[ ] for arrays!

  • When out of memory, new returns NULL.

  • If ptr is NULL,

    
        delete ptr;
    
    

    is harmless.


<<< More function default parameters     Index     Destructors >>>