I have an array of float rtmp1[NMAX * 3][3], and it is used as rtmp1[i][n], where n is from 0 to 2, and i is from 0 to 3 * NMAX - 1. However, I would like to convert rtmp1 to be rtmp1[3 * 3 * NMAX]. Would addressing this new 1D array as rtmp1[3 * i + n] be equivalent to rtmp1[i][n]? Thanks in advance for the clarifications.
A:
Yes, but what are you trying to prove by doing this? rtmp1[i][n] would most likely have better execution time and is easier to read.
"Actually, you'd want to use rtmp[i + 3*n]" what is the difference? All you are doing is swapping addresses.
Will
2009-08-10 19:41:49
I'm trying to make rtmp1 to be dynamically allocated to be a 1D array, as I don't know its size until run-time. NMAX is a really large number to be allocated statically, which the current implementation is very ghetto when it comes to memory consumption.
stanigator
2009-08-10 19:54:23
A:
I'm not sure it won't break some alias prevention rules. My reading is that it is OK, but I've already been wrong and the whole area is sometimes confusing and bold on to know of which of two conflicting rule in different part of the standard takes priority.
Exemple:
typedef float Point[3];
void f(float* tab, Point* pt)
{
(*pt)[2] = 6;
// I don't think the compiler can assume that (*pt)[2] isn't modified by
tab[5] = 3.141592;
}
// context which give a problem if I'm wrong.
float rtmp1[NMAX*3][3];
float *ptr = &rtmpl[0][0];
f(ptr, rtmpl[1]);
AProgrammer
2009-08-10 19:54:21
+4
A:
rtmp1[i][n] is equivalent to rtmp1[i*NMAX + n]
See http://www.cplusplus.com/doc/tutorial/arrays/, where your NMAX is their width.
bill weaver
2009-08-10 19:58:11
The main goal of what I was trying to do is to convert from a 2D array to a pseudo-2D array. Your answer is the one that I accept. Thanks.
stanigator
2009-08-10 21:14:22