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;
}
#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
Post a Comment