I have an issue understanding some things about arrays in C++.
If I have array with 3 rows and 4 colomns and I create them as 1d array and accessing each row data by looping through the array by 4 each time . does this way save me time in comparison with 2d approach that take much more allocation.
so instead of this:
int **array = new int * [3];
for(int i = 0; i < 4; i++) {
array[i] = new int [4];
}
I make this :
int *array = new int [3 * 4];
and I access each row data this way : rows = 3 , colomns = 4 :
for(int i = 0;i < 3; i++) {
for(int j = 0;j < (3 * 4); j++) {
cout << "row : << i << " , 4: " array[i * j];
}
}
does this way save time for my program better than 2d or no?
Is it a bad approach or a good approach writhing my 2d array in 1d array like I what did?
NOTE :
My array will not be dynamic, the size of my array will be know before creating it, I will use it in my neural network project. and my concerns and focus are on speed.