2d array smart pointer code example
Example: c++ smart pointer 2d array
#include <iostream>
#include <memory>
using namespace std;
int main(){
int** pi=new int*[5];
int counter=0;
for(int j=0;j<5;j++){
int* i=new int[5];
for(int j=0;j<5;j++)
i[j]=j+counter;
counter=counter+5;
pi[j]=i;
for(int k=0;k<5;k++)
cout<<pi[j][k]<<" ";
cout<<endl;
}
cout<<endl;
unique_ptr<int*[]> smp_pi(new int*[5]);
counter=0;
for(int j=0;j<5;j++){
unique_ptr<int[]> smp_i(new int[5]);
for(int k=0;k<5;k++){
smp_i[k]=counter+k;
cout<<smp_i[k]<<" ";
}
counter=counter+5;
cout<<endl;
smp_pi[j]=&smp_i[0];
}
cout<<endl;
for(int j=0;j<5;j++){
for(int k=0;k<5;k++)
cout<<smp_pi[j][k]<<" ";
cout<<endl;
}
return 0;
}