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 |
What language are you using?
|
Oh..sorry to not to mention that..I am using C
|
My C is real weak but I think this will work.
Code:
|
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.. |
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; |
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)); |
Malloc and calloc difference doesn't lies in contiguousness, which is pointless anyway, but in (un)initializing the allocated data.
|
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]; 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][]) Other than those, you have to compute your own indexes. Groetjes, Kees-Jan |
Should you use Java, you could write this nice construction :) :
Code:
int table[][][]= new int[rows][columns][depth]; |
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 |
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++) ;) |
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. |