LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to allocate memory for a multidimensional array ? (https://www.linuxquestions.org/questions/programming-9/how-to-allocate-memory-for-a-multidimensional-array-256317/)

indian 11-18-2004 02:39 AM

How to allocate memory for a multidimensional array ?
 
Hi,
Does any one knows how to allocate and free memory for multi-dimensional array say of dimensions a*b*c ? I am able to do this for uni-dimensional but not for 3-D array.

Thanks in anticipation

theYinYeti 11-18-2004 03:06 AM

What language are you using?

indian 11-18-2004 03:07 AM

Oh..sorry to not to mention that..I am using C

RandomLinuxNewb 11-18-2004 03:08 AM

My C is real weak but I think this will work.

Code:


int *foobar;
int size = a*b*c;

foobar = (int *)malloc(size * sizeof(int));

free(foobar);

I just found this page which seems like it would be helpful, here's the link

indian 11-18-2004 04:22 AM

Thank for response but this won't work :)...this is equivalent to creating an array of size a + b + c....i.e

int arr[a+b+c];

what I want is to dynamically allocate memory for

int arr[a][b][c]

Thanks for showing interest though..

theYinYeti 11-18-2004 04:46 AM

No, I think RandomLinuxNewb is right.
But then you'll have to compute the index yourself, because it is actually a one-dimension array with the size a*b*c. So for accessing the place [x][y][z], you'd have to access index [(x*a)+(y*b)+z], or something like that.

I can be wrong, but IMHO, technically speaking, an array like you want is a pointer (array of dimension 3) on "things" that are themselves pointers (array of dimension 2) on "things" that are in turn pointers (array of dimension 1) on ints. I don't think this can be written in one row. You'd have to write something like (bear with me, it's been years since I wrote a single line of C):
Code:

int ***myArray;
myArray = (int***) malloc(a * sizeof(int**));
for (i=0; i<a; i++) {
  myArray[i] = (int**) malloc(b * sizeof(int*));
  for (j=0; j<b; j++) {
    myArray[i][j] = (int*) malloc(c * sizeof(int));
  }
}

Yves.

Winno 11-18-2004 07:53 AM

Correct me if I'm wrong, but I probably would use calloc instead of malloc myself. Malloc does not guarantee contiguous memory allocation, but calloc does. This is closer to what a fixed array (eg int arr[6];) would do. Malloc may work, but I'm not sure.

To use calloc, you have int *i = (int*)calloc(size, sizeof(int));

jlliagre 11-18-2004 08:31 AM

Malloc and calloc difference doesn't lies in contiguousness, which is pointless anyway, but in (un)initializing the allocated data.

kees-jan 11-18-2004 08:56 AM

There is truth in all posts :-)

In order for the C compiler to be able to compute indexes, it needs to know the size of your array. So, to have a 3D array, you can do something like:
Code:

int a[4][3][2];
Memory will be pre-allocated for you. The values of 4, 3 and 2 need to be known compile time.

This can be relaxed a bit: The size of the last index need not be known. So you could for example do something like:
Code:

void func1(int a[4][3][])
{
  //Do something with 3d array a
}

int main()
{
  int x=27; // size of last dimension can be dynamic
  func1((int[3][2][])malloc(4*3*x*sizeof(int)));
}

Lastly, you can create the array of pointers to arrays of pointers to arrays of int technique described by theYinYeti

Other than those, you have to compute your own indexes.

Groetjes,

Kees-Jan

jlliagre 11-18-2004 10:04 AM

Should you use Java, you could write this nice construction :) :
Code:

int table[][][]= new int[rows][columns][depth];

indian 11-18-2004 03:51 PM

Hi ,
this is about the "yinyeti"'s code....Well I think this looks absolutely correct.But now How shud i free the memory ? shud I just write

free(myarray);

I am not sure that this will work...
THanks

jlliagre 11-18-2004 05:06 PM

TheYinYeti's code is indeed correct, this is the way you can allocate multi dimensional arrays in C.
To free them, you need to reverse the work, by deallocating each single block like this.

Code:

for(i=0; i<a; i++)
{
  for(j=0; j<b; j++)
  {
    free(myArray[i][j]);
  }
  free(myArray[i]);
}
free(myArray);

In Java, to free the same array, here's the sample code :
;)

Winno 11-19-2004 03:37 AM

In Java you could do just myArray = null;. The garbage collector will come and trash it :D.

In C, you must use free as indicated above, but as an extra measure, I'd set myArray = NULL; to rid dangling pointers. Don't just set something to NULL, or you'll get a memory leak.


All times are GMT -5. The time now is 04:16 AM.