Re: Addressing multidimensional arrays



"Jujitsu Lizard" <jujitsu.lizard@xxxxxxxxx> writes:
"Baboon" <nospam@xxxxxxxxxxxxxx> wrote in message
news:3083032.OGUtdWV9SE@xxxxxxxxxxx

Suppose I have a multidimensional array, like so

T arr[N][M];

and in one place of my code it makes sense to access all N*M elements
via one single index.

Now, if I understand the C standard correctly, even though all elements
are continuous in memory (they are, are they not?), indices can only ever
be 0..N-1 and 0..M-1, respectively.

But, if I have a pointer T *p pointing at arr[0][0], and I increment p
N*M times, and access arr along the way, would that invoke UB? I mean,
arr is still one single object, right?

What if I use p[i] with i=0..M*N-1 ?

The trick you are suggesting is done every day of the week with
various arrays. If you have an array a[N][M], C is guaranteed to
store the M*N elements contiguously in row-major order.

The link that the other responder posted cited various obscure
optimizations that might break things. This is true, but it rarely
happens in practice. The reason is that when people are messing with
arrays using a single index (one dimensional) when the array is
defined with multiple dimensions, what normally happens is that one
calls a function that accepts a T * pointer. It is rare that someone
defines an array a[N][M] and then uses a[N-1][M] or something like
this in that form. It normally doesn't occur that way in the code.

This code should be fine:

#define M 33
#define N 41

long arr[N][M];

void f(long *p)
{
unsigned i, j;

for (i=0; i<N; i++)
{
for (j=0; j<M; j++)
{
p[i * M + j] = 0;
}
}
}

int main(void)
{
f(arr);

arr isn't a long*

return(0);
}

This is done every day of the week.

People write code that fails to compile without a diagnostic
that's telling them they're doing something they shouldn't
every day of the week?

Quite probably true. Doesn't make it right.

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.
.



Relevant Pages

  • Re: Addressing multidimensional arrays
    ... N*M times, and access arr along the way, would that invoke UB? ... The reason is that when people are messing with arrays using a single index when the array is defined with multiple dimensions, what normally happens is that one calls a function that accepts a T * pointer. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)
  • Re: Evaluating unary *
    ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... the expression &arr requires no special handling beyond insertion of the appropriate address into a suitable register. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... It does create a pointer value which points at arr itself, and treats the entirety of arr as the first element in an array containing exactly one element of type int. ...
    (comp.std.c)
  • Re: multi dimensional arrays as one dimension array
    ... Are you of the opinion that one or both of memcpy(p, arr, sizeof arr) ... way of converting a two-dimensional array into a one-dimensional ... &arr) is supposed to be pointer constrained legally to range over ... arrays of character type and other objects treated as arrays ...
    (comp.lang.c)
  • Re: can .range return a 1D array?
    ... LBound of arrays that are declared like this: ... Dim ptr As Long ... ' Variant parameter that has received the array ... ' Sets Ary's LBound to NewBound, ...
    (microsoft.public.excel.programming)