LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2004, 03:39 AM   #1
indian
Member
 
Registered: Aug 2004
Posts: 137

Rep: Reputation: 15
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
 
Old 11-18-2004, 04:06 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 63
What language are you using?
 
Old 11-18-2004, 04:07 AM   #3
indian
Member
 
Registered: Aug 2004
Posts: 137

Original Poster
Rep: Reputation: 15
Oh..sorry to not to mention that..I am using C
 
Old 11-18-2004, 04:08 AM   #4
RandomLinuxNewb
Member
 
Registered: Oct 2003
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
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

Last edited by RandomLinuxNewb; 11-18-2004 at 04:17 AM.
 
Old 11-18-2004, 05:22 AM   #5
indian
Member
 
Registered: Aug 2004
Posts: 137

Original Poster
Rep: Reputation: 15
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..
 
Old 11-18-2004, 05:46 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 63
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.
 
Old 11-18-2004, 08:53 AM   #7
Winno
Member
 
Registered: Sep 2003
Location: Australia
Distribution: Fedora Core 3 / Mandrake 10.1 / Gentoo 2005.0
Posts: 100

Rep: Reputation: 15
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));
 
Old 11-18-2004, 09:31 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Malloc and calloc difference doesn't lies in contiguousness, which is pointless anyway, but in (un)initializing the allocated data.
 
Old 11-18-2004, 09:56 AM   #9
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
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

Last edited by kees-jan; 11-18-2004 at 11:32 AM.
 
Old 11-18-2004, 11:04 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Should you use Java, you could write this nice construction :
Code:
int table[][][]= new int[rows][columns][depth];
 
Old 11-18-2004, 04:51 PM   #11
indian
Member
 
Registered: Aug 2004
Posts: 137

Original Poster
Rep: Reputation: 15
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
 
Old 11-18-2004, 06:06 PM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
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 :
 
Old 11-19-2004, 04:37 AM   #13
Winno
Member
 
Registered: Sep 2003
Location: Australia
Distribution: Fedora Core 3 / Mandrake 10.1 / Gentoo 2005.0
Posts: 100

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

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot allocate memory Terroth Linux - General 13 02-09-2005 06:22 AM
allocate more memory? sockknitter Debian 3 11-06-2004 06:23 PM
return index of element in java multidimensional array dave bean Programming 8 11-28-2003 12:00 PM
multidimensional array (php) niehls Programming 2 01-31-2003 06:34 AM
PHP: Multidimensional array problem lhoff Programming 1 06-10-2002 03:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration