1#include <iostream>
2#include <memory>
3using namespace std;
4
5int main(){
6 //this just builds a 2D array pi that looks like
7 //0 1 2 3 4
8 //5 6 7 8 9
9 //10 11 12 13
10 //... 24
11 int** pi=new int*[5];
12
13 //this counter is augmented by 5 in every loop,
14 //for the value to be 0...,5,..10etc
15 int counter=0;
16 for(int j=0;j<5;j++){
17 int* i=new int[5];
18 for(int j=0;j<5;j++)
19 i[j]=j+counter;
20 counter=counter+5;
21
22 pi[j]=i;
23 //just to print out the array
24 for(int k=0;k<5;k++)
25 cout<<pi[j][k]<<" ";
26 cout<<endl;
27 }
28 cout<<endl;
29
30 //trying the same thing using smart pointers
31 unique_ptr<int*[]> smp_pi(new int*[5]);
32 counter=0;
33 for(int j=0;j<5;j++){
34 unique_ptr<int[]> smp_i(new int[5]);
35 for(int k=0;k<5;k++){
36 smp_i[k]=counter+k;
37 cout<<smp_i[k]<<" ";
38 }
39 counter=counter+5;
40 cout<<endl;
41 smp_pi[j]=&smp_i[0];
42 //smp_pi[j]=smp_i; //this does not compile. why?
43 }
44 cout<<endl;
45
46 for(int j=0;j<5;j++){
47 for(int k=0;k<5;k++)
48 cout<<smp_pi[j][k]<<" ";
49 cout<<endl;
50 }
51
52 return 0;
53}