array performance

F

Frank Pool

Hi,

I have on large threedimensional array
int largeArray[1024][64][4];
In a particular function I only need a part of this array
So I'm using a new variable and assign it the follwoing way:

int (*smallArray)[64][4];
smallArray = &largeArray[42];

Is the following code correct if I want to get the value of an element ?
int x = smallArray[0][0];


Is there a performance difference between retriveing many elements of the
small and the large array in my function ?

Thanks,
Frank
 
M

Malcolm

Frank Pool said:
I have on large threedimensional array
int largeArray[1024][64][4];
In a particular function I only need a part of this array
So I'm using a new variable and assign it the follwoing way:

int (*smallArray)[64][4];
smallArray = &largeArray[42];

Is the following code correct if I want to get the value of an element ?
int x = smallArray[0][0];
I don't think so. smallArray is a pointer to a list of int [64][4] s. So
smallArray[0] would be the first 2d array (&largeArray[42]), smallArray[1]
would be the next (&largeArray[43]) and so on. So smallArray[0][0] actually
returns a list of 4 integers, and you need (*smallArray)[0][0];

However I have been programming in C for years, and had to think about this.
This is the problem with mutli-dimensional arrays. Beyond the very basics,
the syntax is horrible and confusing and even an experienced programmer will
find it difficult to understand your program.
Is there a performance difference between retriveing many elements of the
small and the large array in my function ?
Depends on the platform. Your huge array probably won't fit in a cache, but
if you can arrange matters so that acesses that are continguous in memory
are also close together in order, you might see some speed-up. Declaring a
pointer to a small part of the array is unlikely to have an impact by
itself, but it might help you organise the code efficently.
 
B

Barry Schwarz

Hi,

I have on large threedimensional array
int largeArray[1024][64][4];

largeArray is an array of 1024 arrays of 64 arrays of 4 int.
In a particular function I only need a part of this array
So I'm using a new variable and assign it the follwoing way:

int (*smallArray)[64][4];

smallArray is a pointer to an array of 64 arrays of 4 int.
smallArray = &largeArray[42];

largeArray[42] is the n-th element (out of the possible 1024 elements)
of largeArray. this element is an array of 64 arrays of 4 int.
&largeArray[42] is the address of this element with the same type as
smallArray.
Is the following code correct if I want to get the value of an element ?
int x = smallArray[0][0];

No. smallArray is a pointer to the first of a series of objects, each
of which is an array of 64 arrays of 4 int. smallArray[0] is the n-th
such object pointed to and is therefore an array of 64 arrays of 4
int. smallArray[0][0] is the n-th array (of the possible 64 arrays)
and has type array of 4 int.

As you have set it up, you need to use (*smallArray)[0][0].

Alternatively, you could consider the following:

largeArray[42] is a particularly object (out of the 1024 that
largeArray consists of) with type 64 arrays of 4 int.

In most contexts, the expression largeArray[42] evaluates to the
address of the first element, that is the address of the first array
of 4 int (out of the possible 64) with type pointer to array of 4 int.
One exception to this rule (there are others) is when the expression
is the operand of the & operators, as it is in your original code.

If you code
int (*smallArray)[4];
you can assign to it with
smallArray = largeArray[42];
and then refer to individual integers with smallArray[j] which I
find to be a much easier notation to read and to type.
Is there a performance difference between retriveing many elements of the
small and the large array in my function ?
A quality of implementation issue which is not part of the language we
discuss here. You can ask in a newsgroup where your compiler is
discussed.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top