CFD Online Logo CFD Online URL
Home > Forums > Main CFD Forum

about multidimensional arrays in C

Register Blogs Members List Search Today's Posts Mark Forums Read

LinkBack Thread Tools Display Modes
Old   June 26, 2003, 15:56
Default about multidimensional arrays in C
Posts: n/a

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] = ...;

  Reply With Quote

Old   June 26, 2003, 17:52
Default Re: about multidimensional arrays in C
Posts: n/a

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.

  Reply With Quote

Old   June 27, 2003, 02:55
Default Re: about multidimensional arrays in C
Posts: n/a

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.

  Reply With Quote

Old   June 29, 2003, 16:37
Default Re: about multidimensional arrays in C
Posts: n/a
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 ?

  Reply With Quote


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
multidimensional Field<> linch OpenFOAM Programming & Development 0 September 13, 2011 07:50
How to represent the pressure evaluated by multidimensional averaging? renyun0511 OpenFOAM Running, Solving & CFD 0 November 4, 2010 03:44
Define Point arrays on a curve selim FLUENT 0 June 4, 2008 10:30
Dynamic multidimensional array in C++? Chen Zhi Main CFD Forum 13 May 22, 2008 21:18
Cannot allocate memory for arrays (1400,1000) Quarkz Main CFD Forum 5 January 27, 2008 07:38

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