-
@I C Everything (& @cooper1200 ),
The old rule applies and is extended.
A 256 x 256 texture might be an array of structures, where each entry is 3 bytes (4 bytes with alpha), basically organizes as 256 rows of 256 pixels (or columns), but is one block of memory addressed as pointer + 256 * sizeof( pixel ) * row + sizeof(pixel) * column (or where row is y and column is x ).
Cache could be as high as 20 Mbytes (maybe even more in some processors), but it's in 2 or 3 levels where interior cache (fastest) might be 64K.
Alignment is key to performance, certainly - but in images alignment is tough (especially an RGB without alpha in 8 bit color).
@cooper1200 you might find this interesting (hopefully not confusing)
Code:
struct ptr
{
int i[100];
};
int main()
{
int a[ 1000 ];
ptr *p = (ptr *) &a[0];
for( int r = 0; r < 10; ++r, ++p )
{
p->i[0] = r;
p->i[1] = r;
}
}
This little (ugly perhaps) code shows that p, of type ptr, can reference rows of a 2D array. Here it sets the first two columns of each row to the row number.
It works the same way if done:
Code:
struct ptr
{
int i[100];
};
int main()
{
int a[ 10 ][ 100 ];
ptr *p = (ptr *) &a[0];
for( int r = 0; r < 10; ++r, ++p )
{
p->i[0] = r;
p->i[1] = r;
}
}
-
Some examples ("%04p" prints a pointer with Microsoft compilers). The 3d matrix "c", could be static or global.
Code:
#include <stdio.h>
int main(int argc, char **argv)
{
int (*a)[5][5]; /* ptr to a 2d matrix, [5][5] */
int *b[5][5]; /* a 2d matrix of pointers to integers */
int c[3][5][5]; /* a 3d matrix of integers */
a = c; /* point a to the 2d matrix at c[0][][] */
printf("%04p, %04p\n", a, c[0]);
a = &c[1]; /* point a to the 2d matrix at c[1][][] */
printf("%04p, %04p\n", a, c[1]);
a++; /* point a to the 2d matrix at c[2][][] */
printf("%04p, %04p\n", a, c[1]);
b[2][4] = &c[1][2][3]; /* set a pointer to integer */
printf("%04p, %04p\n", b[2][4], &c[1][2][3]);
return(0);
}
-
Niccolo that is exactly the main reason I asked this question. I began to realize that the concept of a multidimensional array at least as shown here in C, is basically an abstract concept that humans use to address/access specific elements in the area in a mathematically uniform fashion.