Thread: Array and other "Special pointers"

  1. #16
    Registered User
    Join Date
    May 2019
    Posts
    214
    @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;
       
       }
    
    }
    Last edited by Niccolo; 06-09-2019 at 01:33 PM.

  2. #17
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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);
    }
    Last edited by rcgldr; 06-09-2019 at 07:47 PM.

  3. #18
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    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.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A "Simple" Array/String Problem. "Help" ASAP needed
    By AirulFmy in forum C Programming
    Replies: 10
    Last Post: 08-19-2015, 04:30 AM
  2. Replies: 39
    Last Post: 07-28-2013, 08:24 PM
  3. Replies: 2
    Last Post: 11-16-2011, 05:55 AM
  4. Replies: 2
    Last Post: 05-10-2005, 07:15 AM
  5. Replies: 9
    Last Post: 12-02-2003, 02:24 PM

Tags for this Thread