LinuxQuestions.org
Review your favorite Linux distribution.
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 02-25-2004, 11:44 PM   #1
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
allocating memory


Hi!
Can any body expalin me the way of allocating memory for a two dimensional array. I mean i will have a pointer(or double pointer) but i should allocate memory using malloc or calloc. When i am accesing data i should follow array indexing like a[1][2];
 
Old 02-26-2004, 12:21 AM   #2
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
for(x = 0;x<20;x++)
for(y = 0;y<20;y++)
a[x][y] = 0;

like this?

or this? from: http://msdn.microsoft.com/library/de...crt_malloc.asp

#include <stdlib.h> /* For _MAX_PATH definition */
#include <stdio.h>
#include <malloc.h>

void main( void )
{
char *string;

/* Allocate space for a path name */
string = malloc( _MAX_PATH );
if( string == NULL )
printf( "Insufficient memory available\n" );
else
{
printf( "Memory space allocated for path name\n" );
free( string );
printf( "Memory freed\n" );
}
}

Last edited by chewysplace; 02-26-2004 at 12:24 AM.
 
Old 02-26-2004, 05:17 AM   #3
doraiashok
LQ Newbie
 
Registered: Nov 2003
Posts: 19

Rep: Reputation: 0
One of the ways to allocate memory for a double pointer is given in the code below,

Code:
// m is the no. of rows
// n is the no. of columns
int **matrix;
matrix = (int **) malloc(sizeof(int*)*m);
for(i=0;i<m;i++)
{
  matrix[i] = (int *) malloc(sizeof(int) * n);
}
 
Old 02-26-2004, 05:39 AM   #4
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Original Poster
Rep: Reputation: 15
Mr. can i access that array using
Matrix[i][j];
 
Old 02-26-2004, 06:50 AM   #5
doraiashok
LQ Newbie
 
Registered: Nov 2003
Posts: 19

Rep: Reputation: 0
Of course you can access the array using Matrix[i][j],

Consider this example,

On the first allocation, let the address of Matrix be as below,

Matrix = 1004 (address value)

so, Matrix + 1 = 1008
Matrix + 2= 1012
...
Matrix + ( m - 1) = ...


now allocating each row,

Matrix[0] = *(Matrix+0) = 2000, (Let us assume some address)
Matrix[1] = *(Matrix+1) = 3000,
.... and so on

after above allocation,

Matrix[0], Matrix[1] ..... will point to the address of n integers.

so,

Matrix[0] + 1= *(Matrix+0)+1 = 2004 (These are addresses)
Matrix[0] + 2= *(Matrix+0)+2 = 2008
.... so on

now,

Matrix[1][1] = *(Matrix[1]+ 1) = *( *(Matrix+1)+1) = 10; (can store an integer value).

Hope you get it....
Thanx.
 
Old 02-26-2004, 06:57 AM   #6
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Original Poster
Rep: Reputation: 15
ok!
Thanks Mr. Ashok
I understood.
Once again thanks.
 
Old 02-26-2004, 08:06 AM   #7
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
depending on what you want to do with the array a double pointer approach is not always the best, because if the array is nXm then each traversal requires n pointer dereferences where each may require a cache refill(if array is big), the amount of memory required to store the array is m*(n+1) if the datasize is the same as pointer size, however, substituting one row for another is no more than a single memory assignment. my preferred method is to create a 1d array m*n in size and access it like array(i,j)=array[i+j*n], traversal is much quicker and it takes less memory but row operations are much more difficult.
 
  


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
Allocating Memory in the kernel arunachalam Linux - Software 4 10-12-2005 09:51 AM
seg. fault when allocating memory via a pointer inside a struct elmafiacs Programming 4 02-20-2005 08:26 AM
Allocating memory for user program variable from kernel module appas Programming 2 08-12-2004 11:52 PM
Allocating bandwidth to applications ilikejam Linux - Networking 2 10-13-2003 06:04 AM
allocating space for new partition ranger12002 Linux - General 12 07-20-2003 04:07 PM


All times are GMT -5. The time now is 08:06 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration