The new operator and 2-D array

Hey, so i came across this code and i hoped someone would explain it to me.

int*val,r,c;
cout<<"enter dimensions";
cin>>r>>c;
val=new int[r*c];
for(int i=0;i<r;i++)
{
cout<<"Enter elements in row"<<i+1<<":";
for(int j=0;j<c;j++)
{
cin>>val[i*c+j];
}
}
what i want to know is why can't we just use val[r][c],instead of val[r*c];
also in the for loops whats the use of i*c+j?
What you have there is a 1D array. You have a function that converts (i,j) into index into that 1D array. There is actually an inverse function too that converts index into (i,j).

If your code has an automatic array, it actually has a 1D array of memory and it does the index computation for you. For example:
1
2
3
4
5
6
7
const size_t R = ...;
const size_t C = ...;
int val[R][C];
...
val[i][j] = 3;
// that is equivalent to
*(&val[0][0] + i*C + j) = 3;

However, your 1D dynamically allocated array does not know anything about row length and
yout int *val does not even know whether it points to single integer or somewhere in array.
You can actually work with array[x][y].
But I personally prefer the "1D array" approach because it's much easier.
Note: Use better variables for row/column - does "i" belong to "r" or "c"? And what even is "r"?
Line 5 says it represents "r" but reader should not dive into the code to find out - it should be more obvious.



lets assume:
1
2
3
4
unsigned int x;   //position - value is changing from 0 to sizeOfX
unsigned int y;
const unsigned int sizeOfX = someValue;    //one dimension of the array
const unsigned int sizeOfY = someOtherValue;


2D array
You need to create is as:
1
2
3
4
int** val = new int*[sizeOfX];  //This is array of pointers
for(int x=0; x<sizeofX; x++){
   int val[x] = new int[sizeOfY];  //Now you have 2D array of int
}

and to free the dynamic memory (never forget to clean after yourself :-)
1
2
3
4
for(int x=0; x<sizeofX; x++){
   delete[] val[x];
}
delete[] val;

Then you can access each element as val[x][y]

"1D array"
create:
int* val = new int[sizeOfX * sizeOfY];
free memory:
delete[] val;
access:
val[x + (y * sizeOfX)];


Now try the same for a 3D array (or add more dimensions).
1
2
3
val[x +               //new lines are only for better readability - you can use it in the code
    y * sizeOfX +
    z * sizeOfX * sizeOfY] = someValue;

And you can't use declaration int** val = new int[sizeOfX][sizeOfY];
Actually you may use it but you would have to create special storage class and overload the operators [] and new. At least I think - I never tried it.


One thing I don't yet know is how the array[][] behave when you want to access it through a pointer. That is something I have to test.
For 1D it's: *(&val[0] + index) = someValue and index = x + y*sizeOfX



also in the for loops whats the use of i*c+j? or mine x + (y * sizeOfX)
Imagine the array would be populated like this - see the 1D array index inside the grid
1
2
sizeOfX = 4
sizeOfY = 3

1
2
3
4
5
  x 0  1  2  3
y
0   0  1  2  3
1   4  5  6  7
2   8  9  10 11

Topic archived. No new replies allowed.