views:

161

answers:

2

Hi. I was looking through the web for a way to dynamically allocate space for 3d matrix, say of int type. And i found many sites concerning 2d matrices, and this one http://www.taranets.com/cgi/ts/1.37/ts.ws.pl?w=329;b=286 And there was this example as shown down. I understood all of above examples but this concerning 3d i cannot. Is creator allocating space in backward way or what else? He starts with allocating space for whole matrix, and then proceeds to Z axis? Thats what i cant understand.

Also if you know any good sites concerning this, post it here, it will be appreciated.

    /* Program 9.4 from PTRTUT10.HTM   6/13/97 */
// http://www.taranets.com/cgi/ts/1.37/ts.ws.pl?w=329;b=286

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

int X_DIM=16;
int Y_DIM=5;
int Z_DIM=3;

int main(void)
{
    char *space;
    char ***Arr3D;
    int y, z;
    ptrdiff_t diff;

    /* first we set aside space for the array itself */

    space = malloc(X_DIM * Y_DIM * Z_DIM * sizeof(char));

    /* next we allocate space of an array of pointers, each
       to eventually point to the first element of a
       2 dimensional array of pointers to pointers */

    Arr3D = malloc(Z_DIM * sizeof(char **));

    /* and for each of these we assign a pointer to a newly
       allocated array of pointers to a row */

    for (z = 0; z < Z_DIM; z++)
    {
        Arr3D[z] = malloc(Y_DIM * sizeof(char *));

        /* and for each space in this array we put a pointer to
           the first element of each row in the array space
           originally allocated */

        for (y = 0; y < Y_DIM; y++)
        {
            Arr3D[z][y] = space + (z*(X_DIM * Y_DIM) + y*X_DIM);
        }
    }

    /* And, now we check each address in our 3D array to see if
       the indexing of the Arr3d pointer leads through in a
       continuous manner */

    for (z = 0; z < Z_DIM; z++)
    {
        printf("Location of array %d is %p\n", z, *Arr3D[z]);
        for ( y = 0; y < Y_DIM; y++)
        {
            printf("  Array %d and Row %d starts at %p\n", z, y, Arr3D[z][y]);
            diff = Arr3D[z][y] - space;
            printf("    diff = %d  ",diff);
            printf(" z = %d  y = %d", z, y);
        }
        putchar('\n');
    }
    putchar('\n');
    system("PAUSE");
    return 0;
}
A: 

What the example should do is point higher dimensions into the contiguous block already allocated.

Liz Albin
And that's what it does do...
Jonathan Leffler
+3  A: 

space is indeed the the memory allocated for the entire matrix.

However, he goes on to create pointers to the areas located in space with

Arr3D = malloc(Z_DIM * sizeof(char **));

Arr3D's purpose is simply a way to access space through indexing (specifying Z,Y,X indexes). Space only has one index, so if you wanted to access matrix element [a][b][c] through space, you would need to convert it to a single space[d] where d is something like a*Y_DIM*Z_DIM + b*Z_DIM+c. So with Arr3D, you can access [a][b][c] through Arr3D[a][b][c].

Arr3D is itself an array of char**, which are pointers to pointers of type char. Arr3D[z] is then a pointer to an array of char pointers. Each Arr3D[z][y] is then set to point to a specific row in the original 3x3 matrix with

Arr3D[z][y] = space + (z*(X_DIM * Y_DIM) + y*X_DIM);

So then with Arr[1][2] you are then accessing a row with z=1, y=2 of the matrix.

Will
Right. The basic concept here is that `space` points to the actual 3D array, flattened into a 1D array, and `Arr3D` is being set up as an index into that array - it just makes accessing it more convenient.
caf
It a bit clearer now, i understand how this works, but i still have trouble visualizing accessing reversed 3d array with ZYX. This example here is more suited for me right now: http://stackoverflow.com/questions/1824363/dynamic-allocation-deallocation-of-2d-3d-arrays. Makes more sense for beginner like me.Thanks Will and others for helping!
Martin Berger