Write a function called my2DAlloc which allocates a two dimensional array. Minimize the number of calls to malloc and make sure that the memory is accessible by the notation arr[i][j].
let's allocate two-dimensional array of dynamic space:
i) Define 'rowptr', a pointer to pointer to int, as the name of the 2D array.
ii) To pointer 'rowptr', let us allocate an array of 'm' elements of type 'pointer to int', Here, 'm' is the number of rows we wish the 2D array to have.
iii) To each element rowptr[i] allocated above, let us allocate an array of 'n' elements of type 'int'. Here, 'n' is the number of columns in each row of the array.
lets take an example of rowptr[4][4].
code:
lets calculate total no of space we required
Total space = no of rows + no of rows*no of rows /*( as we can see in above diagram)*/
i) Define 'rowptr', a pointer to pointer to int, as the name of the 2D array.
ii) To pointer 'rowptr', let us allocate an array of 'm' elements of type 'pointer to int', Here, 'm' is the number of rows we wish the 2D array to have.
iii) To each element rowptr[i] allocated above, let us allocate an array of 'n' elements of type 'int'. Here, 'n' is the number of columns in each row of the array.
lets take an example of rowptr[4][4].
code:
int ** My2DAlloc ( int rows , int cols ) { int ** rowptr = ( int ** ) malloc ( rows * sizeof ( int * )); for ( int i = 0 ; i < rows ; ++ i ) rowptr[ i ] = ( int * ) malloc ( cols * sizeof ( int )); return arr ; }Above method use malloc function (rows + 1) times.Too much use of malloc function will affect the efficiency of the program.How to avoid this ?
Allocate all space once and then move pointer to appropriate position.
lets calculate total no of space we required
Total space = no of rows + no of rows*no of rows /*( as we can see in above diagram)*/
int header = rows * sizeof(int*); //no of rows int data = rows * cols * sizeof(int); //no of rows*no of rows int** rowptr = (int**)malloc(header + data); // total space
now we have to skip no of rows and then store data
let take previous example of rawptr[4][4]
Total space = no of rows + no of rows*no of rows
Total space = 4 + 4*4=20
now we have to allocate first 4 space for row array
so we have to skip till no of rows. To do this we are using variable buf.
data=4*4*4=64
total space allocated for rowptr is 64+16=80 and assume address of rowptr is 100
but we have to skip space till no of rows, for that we are using variable buf.
buf=100+16=116
pass 0:
rowptr[k] = buf + k*cols=116+0*4=116 //Here we Assume Size of int is 4.
pass 1: //Therefore every address is increment by 4.
rowptr[k] = buf + k*cols=116+1*4=132
pass 2:
rowptr[k] = buf + k*cols=116+2*4=148
pass 3:
rowptr[k] = buf + k*cols=116+3*4=164
done!!
we did it in one malloc function call.
final code:
output:-let take previous example of rawptr[4][4]
Total space = no of rows + no of rows*no of rows
Total space = 4 + 4*4=20
now we have to allocate first 4 space for row array
so we have to skip till no of rows. To do this we are using variable buf.
int* buf = (int*)(rowptr + rows);now we have to allocate address for row array.
int header = rows * sizeof(int*); int data = rows * cols * sizeof(int); int** rowptr = (int**)malloc(header + data); int* buf = (int*)(rowptr + rows); int k; for (k = 0; k < rows; ++k) rowptr[k] = buf + k*cols;
lets assume size of int is 4 and we have 4 rows and columns
header=4*4=16data=4*4*4=64
total space allocated for rowptr is 64+16=80 and assume address of rowptr is 100
but we have to skip space till no of rows, for that we are using variable buf.
buf=100+16=116
pass 0:
rowptr[k] = buf + k*cols=116+0*4=116 //Here we Assume Size of int is 4.
pass 1: //Therefore every address is increment by 4.
rowptr[k] = buf + k*cols=116+1*4=132
pass 2:
rowptr[k] = buf + k*cols=116+2*4=148
pass 3:
rowptr[k] = buf + k*cols=116+3*4=164
done!!
we did it in one malloc function call.
final code:
#include <stdio.h> int ** rowptr,* buf,header,data,rows,cols,k,i, j; void ** malloc2d(int rows, int cols) { header = rows * sizeof(int*); data = rows * cols * sizeof(int); rowptr = (int**)malloc(header + data); buf = (int*)(rowptr + rows); for (k = 0; k < rows; ++k) rowptr[k] = buf + k*cols; } void free2d(int** rowptr) { free(rowptr); } int main( ) { malloc2d(3,4); for( i = 0; i < 3; i++) for(j = 0; j < 4; j++) { rowptr[i][j] =1; printf("%d",rowptr[i][j]); } free2d(rowptr); return 0; }
111111111111
0 comments: