Allocate and Delete 2D array by using new and delete operator in C++.

Allocate and Delete 2D array by using new and delete operator in C++.

#include<iostream>
#define ROW 5
#define COL 3

using namespace std;

int main()
{
       int **arr=new int*[ROW];  /* This line allocates 5 rows elements and return 1st rows address. */
       for(int i=0;i<ROW;i++)
           arr[i]=new int[COL];  /* This line allocate the column area for every rows.*/


     for(int i=0;i<ROW;i++)
        delete[] arr[i];  /* This line delete the every area of each rows of array. */
    delete[] arr;  /*finally delete the those rows which allocate first time.*/

    system("PAUSE");
    return 0;
}


Comments

Popular posts from this blog

uva 679 - Dropping Balls Solution

uva 481 - What Goes Up Solution

uva-10077 Solution --- The Stern-Brocot Number System