dynamic memory mgmt c++

#include<iostream>
#include<fstream>
using namespace std;

int main() {

//allocation of dynamic memory

//allocation of array of int*
int ** mat = new int*[5];
for (int i = 0; i < 5; i++) {
//allocation of array of int
mat[i] = new int[5];
for (int j = 0; j < 5; j++) {
mat[i][j] = i + j;
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}

//deallocation of dynamic memory
//deallocate array of int
for (int j = 0; j < 5; j++) {
delete[] mat[j];
}
//deallocate array of int*
delete[] mat;

return 0;
}

Leave a comment