Consider the following code:
#include <Windows.h>
#include <stdio.h>
void print2d(int** A,int Y,int X)
{
for(int i=0;i<Y;i++)
{
for(int j=0;j<X;j++)
printf("%d ", A[i][j]);
printf("\n");
}
}
int main()
{
int Y=2;
int X=4;
int** tab2d=new int*[Y];
for(int i=0;i<Y;i++)
{
tab2d[i]=new int[X];
for(int j=0;j<X;j++)
tab2d[i][j]=0;
}
int* row=new int[X];
for(int i=0;i<X;i++)
row[i]=i;
int option=0;
switch(option)
{
case 0:
tab2d[Y-1]=row;
case 1:
for(int i=0;i<X;i++)
tab2d[Y-1][i]=i;
}
print2d(tab2d,Y,X);
free(tab2d);
free(row);
system("pause");
}
Given that in real code, there will be a lot of iterations on 2d array, is there any advantage to either case 0 or case 1? Assigning new row pointer seems like a nicer approach, but wouldn't it space my array rows all over the memory?