CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Main CFD Forum (
-   -   about multidimensional arrays in C (

Adriana June 26, 2003 15:56

about multidimensional arrays in C

what is more efficient (time memory) for 2D or 3D arrays in C ?

a) float ***A; A = ()calloc(...); ... A[layer][row][col] = ...;


b) float *B; B = ()calloc(...); ... B[col + Ncols*row + Ncols*Nrows*layer] = ...;


Ravi June 26, 2003 17:52

Re: about multidimensional arrays in C

I would choose b. I have used both before and figured b is more eficient than a. Probably, b implies, we have contigous memory locations and hence easy to access.

Obviously, for ease of implementation a is better and you can comprehend it easily when you look at your code at a later date.


jdc June 27, 2003 02:55

Re: about multidimensional arrays in C

I agree with Ravi about choosing b for efficiency.

To really account for efficiency, you need to loop on the first index, eg:

for (int ic=0; ic<NumberOfColumns; ic++) { for (int ir=0; ir<NumberOfRows; ir++) {


B(ind) } }

The ideal being:

for (int ind=0; ind<NumberOfColumns*NumberOfRows; ind++) { B(ind) }

which implied that B is dimension to your problem.


Adriana June 29, 2003 16:37

Re: about multidimensional arrays in C
thanks Ravi and Julien.

I have another question. If the option b) is used many times inside a loop, the two products and three additions for the index can reduce the speed ?


All times are GMT -4. The time now is 08:56.