Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tridimensionnal array... 1

Status
Not open for further replies.

fl0ra

MIS
Jul 27, 2004
93
FR
Hello,
I usually have quite a clear view of that kind of thing but today I'm just messed up... :)

I want to make an on the fly tridimensionnal array.
so what about that

Code:
double *** stuff;
stuff=malloc(3*2*5*sizeof(double)); // for a double stuff[3][2][5]

is it gonna work?
of do I also need to malloc and fill the ** stuff; and * stuff? (that is what I guess...)
 
it is fine guys.
I had a ciggie and manage to get it :D


Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        double *** MM;
	int i,j,k;

	MM=(double ***)malloc(2*sizeof(double));

	for (i=0;i<2;i++)
	{
		MM[i]=(double **)malloc(5*sizeof(double));
	}

	for (i=0; i<2 ; i++ )
	{
		for (j=0; j<5 ; j++ )
		{
			MM[i][j]=(double *)malloc(3*sizeof(double));
		}
	}


	for (i=0;i<2;i++)
	{
		for (j=0;j<5;j++)
		{
			for (k=0;k<3;k++)
			{
				MM[i][j][k]=(double)(i+j+k);
			}
		}
	}

	for (i=0;i<2;i++)
	{
		for (j=0;j<5;j++)
		{
			for (k=0;k<3;k++)
			{
				printf("MM[%d][%d][%d]=%lf\n",i,j,k,MM[i][j][k]);
			}
		}
	}
}
 
This:
Code:
MM=(double ***)malloc(2*sizeof(double));
would be better if written like this:
Code:
MM = malloc(2 * sizeof [COLOR=red]*MM[/color]);

Likewise,
Code:
MM[i]=(double **)malloc(5*sizeof(double));
would be better if written like this,
Code:
MM[i] = malloc(5 * sizeof [COLOR=red]*MM[i][/color]);

And of course,
Code:
MM[i][j]=(double *)malloc(3*sizeof(double));
would be better if written like this,
Code:
MM[i][j] = malloc(3 * sizeof [COLOR=red]*MM[i][j][/color]);

Code:
printf("MM[%d][%d][%d]=%[COLOR=red]l[/color]f\n",i,j,k,MM[i][j][k]);
You may have been thinking of [tt]scanf[/tt] because in pre-C99 the [tt]l[/tt] is undefined behavior, although it is a common language extension.

It would also be best to check the return value of malloc before continuing to use it, and free everything before you are done.
 
What you are now using is actually array of arrays of pointers. A real "three-dimensional array" could be described as follows:

Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
double (*MM)[5][3];
int i,j,k;

   MM=(double (*)[5][3])malloc(2*sizeof(*MM));

   for (i=0;i<2;i++)
   {
      for (j=0;j<5;j++)
      {
         for (k=0;k<3;k++)
         {
            MM[i][j][k]=(double)(i+j+k);
         }
      }
   }

   for (i=0;i<2;i++)
   {
        for (j=0;j<5;j++)
      {
         for (k=0;k<3;k++)
         {
             printf("MM[%d][%d][%d]=%lf\n",i,j,k,MM[i][j][k]);
         }
      }
   }

   free(MM);

return(0);
}

Note, that boundaries only of the outer index could be changed dynamically in this case.
 
DaveSinkula:
ok thx...
but why is it???
Code:
MM = malloc(2 * sizeof(*MM));
and not
Code:
MM = malloc(2 * sizeof(MM));

I am confused!!!
 
Code:
MM = malloc(2 * [red]sizeof(*MM)[/red]);
I mentally read this "the size of the thing pointed to by MM". (Perhaps "the size of the object pointed to by MM" would be better.)

You are requesting from [tt]malloc[/tt] a number of objects and as such you want to use the size of the object(s) you are requesting, not the size of the pointer MM. If the object itself happens to be a pointer, this is still correct.
 
Ok cheers...
Now if I want to free it from memory do I simply call
Code:
free(MM);

or do I have to do:
Code:
for (i=0; i<2 ; i++ )
{
  for (j=0; j<5 ; j++ )
  {
    free(MM[i][j]);
  }
  free(MM[i]);
}
free(MM);
 
It's three dimensionally allocated. So to free all the
memory you have allocated you have to go through the steps you performed to create it in reverse.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

int ***allocArray(int sz1, int sz2);
void freeit(int ***array,int r2sz, int r3sz);
void printit(int ***array,int sz1, int sz2);

int main(void) {
int ***foo;
                srand(time(NULL));
                foo = allocArray(12,120);
		printit(foo,12,120);
		freeit(foo,12,120);
return 0;
}

int ***allocArray(int sz1, int sz2) {
int y, x, v;
int ***new = malloc(sz1 * sizeof(int **));

                      for (y=0 ; y < sz1 ; y++) {
		          new[y] = malloc(sz2 * sizeof(int *));
		          for (x=0 ; x < sz2 ; x++) {
			      new[y][x] = malloc(1 + sizeof(int));
			  }
		      }
return new;
}

void printit(int ***array,int sz1, int sz2) {
int y, x;
             printf("Head pointer at array = %p\n",array);

             for (y=0 ; y < sz1 ; y++) {
	         printf("Row pointer at: %p\n",array[y]);
		 for (x=0 ; x < sz2 ; x++) {
		     printf("Column pointer at %p",array[y][x]);
		     *array[y][x] = (int)(1 + rand() % 1200);
		     printf("\nAssigned value = %d\n",*array[y][x]);
		 }
	     }
}

void freeit(int ***array,int r2sz, int r3sz) {
int x, n;


          for (x=0 ; x < r2sz ; x++) {
              for (n=0 ; n < r3sz ; n++) {
                  free(array[x][n]);
              }
              free(array[x]);
          }
free(array);
}

Quick and dirty.
 
cheers for that dude.
that is what I thought
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top