// Example with New[] and Delete[]
#include <iostream>
using namespace std;

int main ()
{
  int i,n;
  int *ptr;
  
  cout << "How many numbers would you like to type? ";
  cin >> i;

/*  
 "New[]" is used to assign a block (an array) of elements of type type, 
  where number_of_elements is an integer value representing the amount of these  
*/
  ptr = new int[i];
/*
  In this case, the system dynamically assigns space for five elements of type int 
  and returns a pointer to the first element of the sequence, which is assigned to ptr. 
  Therefore, now, ptr points to a valid block of memory with space for five elements of type int.  
*/

  if (ptr == 0)
    cout << " Error: memory could not be allocated";
  else
  {
    for (n=0; n<i; n++)
    {
      cout << " Enter number: ";
      cin >> ptr[n];
    }
    
/* 
   The first element pointed by ptr can be accessed either with the expression ptr[0] 
   or the expression *ptr. Both are equivalent as has been explained in the section about pointers.
   The second element can be accessed either with ptr[1] or *(ptr+1) and so on...    
*/
    cout << endl << " You have entered: ";
    for (n=0; n<i; n++) cout << ptr[n] << ", ";
    
/* 
    "Delete[]" should be used to delete memory allocated for arrays of elements    
*/
    delete[] ptr;
  }

    std::cin.get();
    std::cin.get();
    return (0);   
}
