CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

about multidimensional arrays in C

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 26, 2003, 15:56
Default about multidimensional arrays in C
  #1
Adriana
Guest
 
Posts: n/a
Hello.

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

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

or

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

Thanks
  Reply With Quote

Old   June 26, 2003, 17:52
Default Re: about multidimensional arrays in C
  #2
Ravi
Guest
 
Posts: n/a
Hi

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.

Ravi
  Reply With Quote

Old   June 27, 2003, 02:55
Default Re: about multidimensional arrays in C
  #3
jdc
Guest
 
Posts: n/a
Hi,

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++) {

ind=ir+ic*NumberOfRows

B(ind) } }

The ideal being:

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

which implied that B is dimension to your problem.

Julien
  Reply With Quote

Old   June 29, 2003, 16:37
Default Re: about multidimensional arrays in C
  #4
Adriana
Guest
 
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 ?

Adriana
  Reply With Quote

Reply


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 Off
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 02: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 06:38


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