Hey all,
I'm trying to implement a function InitMatrix which suppose to return a pointer to a new created 3d array of the size determined by x,y and z.
I have a struct which represents a matrix and should be able to deal with some functions required, like setting the range of the matrix.. etc..

typedef struct Struct_Matrix {
int ***array;
//..
//..
} Struct_Matrix;

Struct_Matrix *InitMatrix(int x, int y, int z) {

}

I think I know how to allocate a 3D array:

int x, y, z;
 int i,j,k;
 int ***array=(int ***)malloc(x*sizeof(int**));
        for(i=0;i<x;i++) {
         array[i]=(int**)malloc(y*sizeof(int*));
          for(j=0;j<y;j++) {
              array[i][j]=(int*)malloc(z*sizeof(int));
          }
        }

I have 3 questions:
1. Is my allocation correct?
2. How can I do it with the function I gave and the struct? what should I return? How to allocate a memory to the struct itself and then its fields?
3. How do I free the memory allocated by this function? lets say by a function called
void Free(array *free);

Thanks!

struct matrix* InitMatrix(int x, int y, int z)
{
   struct matrix* m = malloc(sizeof(struct matrix));
   // initialize the structures elements

   <snip>
   return m;
}

Free it in reverse orderof allocating it.

Thanks man.
Is this is a legal freeing? I have a feeling that something is wrong and I'll get a leak.

void Free(array *free) {
int i,j;
    for(i=0;i<free->x;i++) {
        for(j=0;j<free->y;j++)
            free(free->matrix[i][j]);
        free(free->matrix[i]);
    }
    free(free->matrix);
}

>>void Free(array *free)
Change the variable name from free to something else because free is a C standard function name, and the compiler will get confused.

Thanks.

btw, among all ways to allocate a 3D matrix. Which is the best way?
I mean.. the best effective way?

Another way to do it is to allocate one large block of memory that is x*y*z*sizeof(int) bytes, then calculate the index into it yourself. Freeing up the array is very easy -- since there is only one pointer there will be only one call to free().

int* allocMatrix(int x,int y,int z)
{
    int* ay = (int *)malloc(x*y*z*sizeof(int));
    memset(ay,0,x*y*z*sizeof(int));
    return ay;
}

void disp(int* ay,int x,int y,int z)
{
    int i,j,k;
    for(i = 0; i < x; i++)
    {
        for(j = 0; j < y; j++)
        {
            for(k = 0; k < z; k++)
            {
                int spot = (i*y*z)+(j*z)+k;
                printf("ay[%d][%d][%d] = %d\n",i,j,k,ay[spot]);
            }
        }
    }

}

int main()
{
    int i,j,k,m;
    int x = 5;
    int y = 3;
    int z = 4;
    int* ay = allocMatrix(x,y,z);
    int spot = 0;

    for(i = m = 0; i < x; i++)
    {
        for(j = 0; j < y; j++)
        {
            for(k = 0; k < z; k++)
            {
                int spot = (i*y*z)+(j*z)+k;
                ay[spot] = m++;
            }
        }
    }
    disp(ay,x,y,z);
    free(ay);
}
commented: Helpful! Thanks Ancient.. always here to help. +1
commented: Effective +6

Many thanks.. helped a lot.
You used memset function to initialize the 3D array to zeros?

I'm asking because when I change the argument 0 in this function to 1 and tries to print the array when initialize.. I get numbers like: 189234 filled into it.
But for some reason, 0 works fine.. and I get value: 0 in the array.

In this case memset() is a lot easier and faster than loops. That can't be used if you want to initialize with some othe value.

Ancient Dragon is there no better way to print the 3D array?
I mean.. here it takes O(n^3) (if n is the dimensions).
Can I print it or set into it.. in a more efficient way?

None that I'm aware of.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.