2

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.

4
  • Shouldn't i be from 0 to (3 * NMAX) - 1 ? Commented Aug 10, 2009 at 19:43
  • Clarification please. Do you mean "i is from 0 to 2" or rtmp1[3 * 4 * NMAX]? Commented Aug 10, 2009 at 19:45
  • @KTC: yes, and I have corrected it @bill weaver: Please clarify your question. Commented Aug 10, 2009 at 19:53
  • I think the answer to your basic question is yes (see my answer below). However, i'm confused by your ranges in the first part of your question. After your edit, i'm more confused by them. Commented Aug 10, 2009 at 20:08

3 Answers 3

4

rtmp1[i][n] is equivalent to rtmp1[i*NMAX + n]

See http://www.cplusplus.com/doc/tutorial/arrays/, where your NMAX is their width.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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.

1 Comment

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.
0

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.