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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic memory allocation for 2D arrays

Status
Not open for further replies.

JackaI

IS-IT--Management
Sep 16, 2001
3
0
0
AU
Hey,

I'm a java programmer, and am quite new to C.
I know how to allocate memory dynamically for most things such as arrays and structures, but don't know how to (if it is possible at all) to allocate memory for a 2D array.

I tried something like the following but it doesn't work:

int** matrix;
.
.
.
matrix = (int**)malloc(sizeof(int) * numVertices * numVertices);

If anyone could help, it would be much appreciated.

Thanx
 
int count = 100;
int** matrix = new int*[count];
for (int k=0;k<100;k++)
matrix[k] = new int[count];
then you can use the matrix as a 2d array
 
sorry forgot this is a c forum. you can use malloc instead of new:
int count = 100;
int** matrix = (int **)malloc(sizeof(int)*count);
for (int k=0;k<100;k++)
matrix[k] = (int *)malloc(sizeof(int)*count);
 
Hey, thanks for the answer...I eventually worked it out by about 1 am :) Got the same result...good to know that I was on the right track...thanks for the response :)
 
If you know the number of columns then you can allocate the 2d array like the following also.

#include <stdio.h>
#include <malloc.h>
int main()
{
int (*p)[3];
int i, j, r;
printf(&quot;Enter number of rows : &quot;);
scanf(&quot;%d&quot;, &r);
p = (int (*)[3]) malloc(sizeof(int) * 3 * r);
for(i = 0; i < r; i++)
{
for(j = 0; j < 3; j++)
{
p[j] = i * 3 + j;
//printf(&quot;\n%u&quot;, &p[j]);
}
//printf(&quot;\n.......%u&quot;, p);
}

for(i = 0; i < r; i++)
{
for(j = 0; j < 3; j++)
{
printf(&quot;%u &quot;, p[j]);
}
putchar('\n');
}
return 0;
}

Regards,
Maniraja S
 
Hey smaniraja,

thanks, but I need to read in a square matrix from a text file which could be any size...the technique used by Jeffray is perfect.
 
You might want to consider allocating one contiguos block of memory. This is helpful in reading the data in from a file and copying one matrix to the other. Below, is a detailed explanation:

int rows = 50, cols = 100; // in general no need to be square
type **buffer; // substitute type with int, for example

buffer = (type **) malloc (rows * sizeof(type *));
if (!buffer) {
// couldn't allocate the memory, handle properly
}
buffer[0] = (type *) malloc (rows * cols * sizeof(type));
if (!buffer[0]) {
// couldn't allocate the memory, handle properly
}
for (int j = 1; j < rows; j++) {
buffer[j] = buffer[j-1] + cols;
}

By doing it in this way, you can do the following:

1. Copy a matrix into another like this, instead of having the double loop:

for (int j = 0; j < rows*cols; j++) {
matrix2[0][j] = matrix1[0][j];
}

2. Also, faster disk reading is done like this:
FILE *fp;
...
fread(buffer[0], sizeof(type), rows*cols, fp);

instead of having to read in row by row.

Hope, this helps.
 
#include <stdio.h>
#include <string.h>
#include <malloc.h>

int **Malloc(int,int);
void MemRelease(char **,int);

void main()
{
int i = 0;

char **b;
b = (char**)Malloc(5,10);

for(i = 0; i < 3; i++)
scanf(&quot;%s&quot;,b);

for(i = 0; i < 3; i++)
printf(&quot;%s\n&quot;,b);

MemRelease(b,5);

}

int **Malloc(int row,int col)
{
int **tmp;
int i = 0;

tmp = (int **)malloc(sizeof(int) * row);

for(i = 0; i < row; i++)
tmp = (int*)malloc(sizeof(char) * col);

return tmp;
}

void MemRelease(char **tmp,int row)
{
int i = 0;

for(i = 0; i < row; i++)
free((void*)tmp);

free((void *)tmp);

printf(&quot;Successfully de allocated memory\n&quot;);
}


Sujin.
 
Yah all done and fine..
but what about increasing the size at runtime..
Here is something of trouble..

#include <stdio.h>
#include <stdlib.h>
void print2d(int** in, int row, int column);
void resize(int ** mat,int row,int column,int new_r,int new_col);
void free2d(int** mat, int row, int column);

int main()
{
//int** i=(int**) calloc(arr_size*arr_size, sizeof(int));
// free(i);
int ** mat = NULL;
int row=5,new_r=8;
int column=5,new_col=8;

mat = (int **) calloc(row, sizeof(int));
for(int i=0;i<column;i++)
mat = (int *) calloc(column, sizeof(int));

for(i=0 ; i< row; i++)
for(int j=0 ; j< column; j++)
mat[j] = i+j;

print2d(mat,row,column);
resize(mat,row,column,new_r,new_col);

print2d(mat,new_r,new_col);
//free2d(mat, new_r, new_col);
}

void print2d(int** in, int row, int column)
{
int i,j;
for(i=0 ; i< row; i++)
{
for(j=0 ; j< column; j++)
{

printf(&quot;%d&quot;,in[j]);
}
printf(&quot;\n&quot;);
}
}
//*************************************
//problem here........... any soultion
//**************************************
void resize(int ** mat,int row,int column,int new_r,int new_col)
{
mat = (int **) realloc(mat, new_r * sizeof(int*));
for(int i=0;i<new_r;i++)
{
if (i<row)
{
mat = (int *) realloc(mat, new_col * sizeof(int));
for(int j=column; j< new_col; j++)
mat[j] = 0;
}
else
mat = (int *) calloc(new_col , sizeof(int));
}
}

void free2d(int** mat, int row, int column)
{
for(int i=0;i<row;i++)
free(mat);
free(mat);
}
 
Just something to think about ... ;-)

Try this:

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

void	print2d(int **,int,int);
int **	resize(int **,int,int);
void	free2d(int **,int);

int main()
{
	int	i = 0,
		j = 0;

	int 	**array,
		row = 5,
		col = 5,
		prow = row + 4,
		pcol = col + 1;

	/* Allocate memory for 2d array */
	array = (int **) calloc(row,sizeof(int *));

	for(i = 0; i < row; i++)
	{
		array[i] = (int *) calloc(col, sizeof(int));
	}

	/* Fill 2d array*/
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col; j++)
		{
			array[i][j] = i + j;
		}
	}

	(void) print2d(array,row,col);

	array =  resize(array,prow, pcol);

	(void) printf(&quot;FILLING NEW\n&quot;);

	/* Fill new 2d array*/
	for(i = 0; i < prow; i++)
	{
		for(j = 0; j < pcol; j++)
		{
			array[i][j] = i+j;
		}
	}

	(void) print2d(array,prow,pcol);
	(void) free2d(array,prow);
}

void print2d(int **array, int row, int col)
{
	int	i = 0,
		j = 0;

	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col; j++)
		{
			printf(&quot;%d &quot;, array[i][j]);
		}

		printf(&quot;\n&quot;);
	}
}

int ** resize(int **array, int prow, int pcol)
{
	int	i = 0;

	if( !( array = (int **) realloc(array, (prow * sizeof(int *)))))
	{
		fprintf(stderr, &quot;ERROR 1\n&quot;);
		exit(1);
	}

	for(i = 0; i < prow; i++)
	{
		if( !(array[i]=(int *)realloc(array[i],(pcol*sizeof(int)))))
		{
			fprintf(stderr, &quot;ERROR 2\n&quot;);
		}
	}

	return array;
}

void	free2d(int **array,int prow)
{
	int	i = 0,
		j = 0;

	for(i = 0; i < prow; i++)
	{
		free(array[i]);
	}

	free(array);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top