"Frank Pool" <Fr********@gmx.net> wrote
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.