Dynamic Memory Allocation for Matrix(2D array) | Member | | Join Date: Apr 2007 Location: Malaysia
Posts: 55
| |
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back.
By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this?
Second Program: -
-
/* Best Method to allocate memory for 2D Array because it's
-
much more flexible
-
*/
-
-
/* int ***matrixptr -> **rowptr -> *rowptr[nrow] -> element of row
-
1st Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-
-> **rowptr -> *rowptr[nrow] -> element of row
-
2nd Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-
-> **rowptr -> *rowptr[nrow] -> element of row
-
3nd Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
*/
-
// How to code this in C
-
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<assert.h>
-
-
/* int **rowptr -- A pointer to pointer to integer
-
This can be explain through graphically.
-
int **rowptr -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
*/
-
static void Allocate(int, int **, int **, int *, int *, int *, int *);
-
//int Symmetric(int *, int *, int *, int *);
-
-
void Add(int **, int **, int *, int *);
-
void Subtract(int **, int **, int *, int *);
-
-
void Display(int **, int *, int *);
-
// int *nrow_ptr = NULL, *ncol_ptr = NULL;
-
-
/* int *nrow_ptr, *ncol_ptr;
-
// Declare the nrow_ptr and ncol pointer
-
nrow_ptr = &nrow;
-
ncol_ptr = &ncol;
-
// Initialized the nrow_ptr and ncol_ptr
-
*/
-
-
-
int main(int argc, char *argv[])
-
{
-
static int **rowptr;
-
static int **rowptr_1;
-
static int **result;
-
-
int nMatrix;
-
int nrow, ncol, nrow_1, ncol_1;
-
// Number of row and column
-
int *nrowptr;
-
int *ncolptr;
-
int *nrow_1ptr;
-
int *ncol_1ptr;
-
// Initialized the pointer pointing to nrow, ncol,
-
// nrow_1, ncol_1 respectively
-
nrowptr = &nrow;
-
ncolptr = &ncol;
-
nrow_1ptr = &nrow_1;
-
ncol_1ptr = &ncol_1;
-
/*
-
Used while loop instead of dowhile because it's recommended
-
by Bjarne Stroustrup
-
*/
-
// ------------------------------------------------------------
-
while(1)
-
{ // Must allocate at least two matrix
-
printf("\nPlease Enter same dimension for two Matrix\n");
-
printf("\nHow many Matrix : ");
-
scanf("%d", &nMatrix);
-
-
printf("\nHow many Row for First Matrix : ");
-
scanf("%d", &nrow);
-
printf("\nHow many Column for First Matrix : ");
-
scanf("%d", &ncol);
-
-
printf("\nHow many Row for Second Matrix : ");
-
scanf("%d", &nrow_1);
-
printf("\nHow many Column for Second Matrix : ");
-
scanf("%d", &ncol_1);
-
// ------------------------------------------------------------
-
Allocate(nMatrix, rowptr, rowptr_1, nrowptr, ncolptr, nrow_1ptr, ncol_1ptr);
-
if (rowptr || rowptr_1 == NULL)
-
{
-
perror("DMA for rowptr and rowptr_1 became NULL when came back from allcoate function");
-
}
-
Add(rowptr, rowptr_1, nrowptr, ncolptr);
-
// Adding the element of matrix
-
Subtract(rowptr, rowptr_1, nrowptr, ncolptr);
-
// Subtract the element of matrix
-
// Display(result, nrow, ncol);
-
// Display the result after successful allocated the memory
-
-
free(rowptr);
-
free(rowptr_1);
-
free(result);
-
}
-
-
-
return 0;
-
-
}
-
// -----------------------------------------------------------
-
static void Allocate(int nMatrix, int **rowptr, int **rowptr_1,
-
int *nrowptr, int *ncolptr,
-
int *nrow_1ptr, int *ncol_1ptr)
-
{
-
// int symmetric;
-
int row_loop, col_loop;
-
-
switch(nMatrix)
-
{
-
case 1:
-
{
-
rowptr = (int **)malloc(sizeof(int **) * (*nrowptr));
-
if (rowptr == NULL)
-
{
-
perror("Dynamic Memory Allocation for row Fails");
-
}
-
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
rowptr[row_loop] = (int *)malloc(sizeof(int *) * (*ncolptr));
-
if (rowptr[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation for column Fails");
-
}
-
}
-
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr[row_loop][col_loop]);
-
}
-
}
-
break;
-
}
-
// ------------------------------------------------------------
-
case 2:
-
{ // Enter the first matrix
-
// do
-
// {
-
printf("\nEnter the First Matrix : \n");
-
// Allocate memory for rowptr **
-
// if nrow == 3, then same as rowptr *[3]
-
rowptr = (int **)malloc(sizeof(int **) * (*nrowptr));
-
if (rowptr == NULL)
-
{
-
perror("Dynamic Memory Allocation for row Fails");
-
}
-
-
// For each rowptr * , allocate ncolumn
-
for (row_loop = 0;row_loop < (*nrowptr); row_loop++)
-
{
-
rowptr[row_loop] = (int *)malloc(sizeof(int *) * (*ncolptr));
-
if (rowptr[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation for column Fails");
-
}
-
}
-
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr[row_loop][col_loop]);
-
}
-
}
-
-
// -----------------------------------------------------------
-
// Enter the second Matrix
-
printf("Enter the Second Matrix : \n");
-
-
// Allocate memory for rowptr_1 **
-
// if nrow == 3, then same as rowptr_1 *[3]
-
rowptr_1 = (int **)malloc(sizeof(int **) * (*nrow_1ptr));
-
if (rowptr_1 == NULL)
-
{
-
perror("Dynamic Memory Allocation Matrix 2 for row Fails");
-
}
-
-
// For each rowptr_1 * , allocate ncolumn
-
for (row_loop = 0; row_loop < (*nrow_1ptr); row_loop++)
-
{
-
rowptr_1[row_loop] = (int *)malloc(sizeof(int *) * (*ncol_1ptr));
-
if(rowptr_1[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation Matrix 2 for column Fails");
-
}
-
}
-
-
for (row_loop = 0; row_loop < (*nrow_1ptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncol_1ptr); col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr_1[row_loop][col_loop]);
-
}
-
}
-
-
// Check the Dimension of Two Matrix
-
/* symmetric = Symmetric(nrowptr, ncolptr, nrow_1ptr, ncol_1ptr);
-
if (symmetric == 1)
-
{
-
printf("The two Matrix is same dimension");
-
}
-
else
-
{
-
printf("The two Matrix is different dimension");
-
}
-
}while(symmetric == 0); */
-
break;
-
}
-
}
-
}
-
// ------------------------------------------------------------
-
int Symmetric(int *nrowptr, int *ncolptr,
-
int *nrow_1ptr, int *ncol_1ptr)
-
{
-
int symmetric;
-
if ((*nrowptr) == (*nrow_1ptr) && (*ncolptr) == (*ncol_1ptr))
-
{
-
symmetric = 1;
-
return symmetric;
-
}
-
else
-
{
-
symmetric = 0;
-
return symmetric;
-
}
-
}
-
// ------------------------------------------------------------
-
void Add(int **rowptr, int **rowptr_1, int *nrowptr, int *ncolptr)
-
{
-
static int **result;
-
int row_loop, col_loop;
-
-
// Allocate memory for result **
-
// if nrow == 3, then same as result *[3]
-
result = malloc(sizeof(int **) * (*nrowptr));
-
if (result == NULL)
-
{
-
perror("Dynamic Memory Allocation result for row Fails");
-
-
}
-
// For each result * , allocate ncolumn
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
result[row_loop] = malloc(sizeof(int *) * (*ncolptr));
-
if (result[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation result for column Fails");
-
}
-
}
-
// Adding the element of two matrix
-
for (row_loop = 0;row_loop < (*nrowptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
-
{
-
*(*(result + row_loop) + col_loop) = *(*(rowptr + row_loop) + col_loop) + *(*(rowptr_1 + row_loop) + col_loop);
-
}
-
}
-
-
Display(result, nrowptr, ncolptr);
-
}
-
-
// ------------------------------------------------------------
-
void Subtract(int **rowptr, int **rowptr_1, int *nrowptr, int *ncolptr)
-
{
-
int row_loop, col_loop;
-
static int **result;
-
-
// Allocate memory for result **
-
// if nrow == 3, then same as result *[3]
-
result = malloc(sizeof(int **) * (*nrowptr));
-
if (result == NULL)
-
{
-
perror("Dynamic Memory Allocation result for row Fails");
-
-
}
-
// For each result * , allocate ncolumn
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
result[row_loop] = malloc(sizeof(int *) * (*ncolptr));
-
if (result[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation result for column Fails");
-
}
-
}
-
// Subracting the element of two matrix
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
-
{
-
*(*(result + row_loop) + col_loop) = *(*(rowptr + row_loop) + col_loop) - *(*(rowptr_1 + row_loop) + col_loop);
-
}
-
}
-
Display(result, nrowptr, ncolptr);
-
}
-
-
// ------------------------------------------------------------
-
void Display(int **result, int *nrowptr, int *ncolptr)
-
{
-
int row_loop, col_loop;
-
// Loop for row and column
-
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
-
{
-
printf("\nThe value of array at [%d][%d] is %d", row_loop, col_loop, *(*(result + row_loop) + col_loop) );
-
}
-
}
-
-
}
-
-
// ------------------------------------------------------------
-
Thanks for your help.
Your help is greatly appreciated by me and others.
| | Member | | Join Date: Apr 2007 Location: Malaysia
Posts: 55
| | | re: Dynamic Memory Allocation for Matrix(2D array)
The first program : -
-
/* Best Method to allocate memory for 2D Array because it's
-
much more flexible
-
*/
-
-
/* int ***matrixptr -> **rowptr -> *rowptr[nrow] -> element of row
-
1st Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-
-> **rowptr -> *rowptr[nrow] -> element of row
-
2nd Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-
-> **rowptr -> *rowptr[nrow] -> element of row
-
3nd Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
*/
-
// How do code this in C
-
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<assert.h>
-
-
-
/* int **rowptr -- A pointer to pointer to integer
-
This can be explain through graphically.
-
int **rowptr -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
*/
-
int Symmetric(int, int, int, int);
-
void Add(int **, int **, int, int, int, int);
-
-
void Display(int **, int , int );
-
// int *nrow_ptr = NULL, *ncol_ptr = NULL;
-
-
/* int *nrow_ptr, *ncol_ptr;
-
// Declare the nrow_ptr and ncol pointer
-
nrow_ptr = &nrow;
-
ncol_ptr = &ncol;
-
// Initialized the nrow_ptr and ncol_ptr
-
*/
-
-
-
int main(int argc, char *argv[])
-
{
-
-
int nMatrix;
-
int nrow, ncol, nrow_1, ncol_1;
-
// Number of row and column
-
-
int row_loop, col_loop;
-
// Loop for row and column
-
-
int **rowptr = NULL; // -- First Matrix
-
// A pointer to pointer to integer
-
-
int **rowptr_1 = NULL; // -- Second Matrix
-
int **result = NULL; // Result of two Matrix
-
int symmetric;
-
/*
-
Used while loop instead of dowhile because it's recommended
-
by Bjarne Stroustrup
-
*/
-
// ------------------------------------------------------------
-
while(1)
-
{ // Must allocate at least two matrix
-
printf("\nPlease Enter same dimension for two Matrix\n");
-
printf("\nHow many Matrix : ");
-
scanf("%d", &nMatrix);
-
switch(nMatrix)
-
{
-
case 1:
-
{
-
printf("How many Row : ");
-
scanf("%d", &nrow);
-
rowptr = (int **)malloc(sizeof(int **) * nrow);
-
if (rowptr == NULL)
-
{
-
perror("Dynamic Memory Allocation for row Fails");
-
}
-
printf("How many Column : ");
-
scanf("%d", &ncol);
-
// For each row, allocated ncol
-
for (row_loop=0;row_loop<nrow;row_loop++)
-
{
-
rowptr[row_loop] = (int *)malloc(sizeof(int *) * ncol);
-
if (rowptr[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation for column Fails");
-
}
-
}
-
-
for (row_loop=0;row_loop<nrow;row_loop++)
-
{
-
for (col_loop=0;col_loop<ncol;col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr[row_loop][col_loop]);
-
}
-
}
-
break;
-
}
-
// ------------------------------------------------------------
-
case 2:
-
{ // Enter the first matrix
-
do
-
{
-
printf("Enter the First Matrix : \n");
-
printf("How many Row : ");
-
scanf("%d", &nrow);
-
rowptr = (int **)malloc(sizeof(int **) * nrow);
-
if (rowptr == NULL)
-
{
-
perror("Dynamic Memory Allocation for row Fails");
-
}
-
printf("How many Column : ");
-
scanf("%d", &ncol);
-
-
for (row_loop=0;row_loop<nrow;row_loop++)
-
{
-
rowptr[row_loop] =(int *) malloc(sizeof(int *) * ncol);
-
if (rowptr[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation for column Fails");
-
}
-
}
-
-
for (row_loop=0;row_loop<nrow;row_loop++)
-
{
-
for (col_loop=0;col_loop<ncol;col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr[row_loop][col_loop]);
-
}
-
}
-
// -----------------------------------------------------------
-
// Enter the second Matrix
-
printf("Enter the Second Matrix : \n");
-
printf("How many row : ");
-
scanf("%d", &nrow_1);
-
rowptr_1 = (int **)malloc(sizeof(int **) * nrow_1);
-
if (rowptr_1 == NULL)
-
{
-
perror("Dynamic Memory Allocation Matrix 2 for row Fails");
-
}
-
printf("How many Column : ");
-
scanf("%d", &ncol_1);
-
-
for (row_loop=0;row_loop<ncol_1;row_loop++)
-
{
-
rowptr_1[row_loop] = (int *)malloc(sizeof(int *) * ncol_1);
-
if(rowptr_1[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation Matrix 2 for column Fails");
-
}
-
}
-
-
for (row_loop=0;row_loop<nrow_1;row_loop++)
-
{
-
for (col_loop=0;col_loop<ncol_1;col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr_1[row_loop][col_loop]);
-
}
-
}
-
break;
-
}while(symmetric == 0);
-
}
-
}
-
// ------------------------------------------------------------
-
// Check the Dimension of Two Matrix
-
symmetric = Symmetric(nrow, ncol, nrow_1, ncol_1);
-
if (symmetric == 1)
-
{
-
printf("\nThe two Matrix is same dimension");
-
}
-
else
-
{
-
printf("\nThe two Matrix is different dimension");
-
}
-
-
// Runtime Error
-
Add(rowptr, rowptr_1, nrow, ncol, nrow_1, ncol_1);
-
// Adding the element of matrix
-
// Display(result, nrow, ncol);
-
// Display the result after successful allocated the memory
-
-
free(rowptr);
-
free(rowptr_1);
-
free(result);
-
}
-
-
-
return 0;
-
-
}
-
// -----------------------------------------------------------
-
int Symmetric(int nrow, int ncol, int nrow_1, int ncol_1)
-
{
-
int symmetric;
-
if (nrow == nrow_1 && ncol == ncol_1)
-
{
-
symmetric = 1;
-
return symmetric;
-
}
-
else
-
{
-
symmetric = 0;
-
return symmetric;
-
}
-
}
-
// ------------------------------------------------------------
-
void Add(int **rowptr, int **rowptr_1, int nrow,
-
int ncol, int nrow_1, int ncol_1)
-
{
-
int row_loop, col_loop;
-
int **result = NULL;
-
-
nrow = nrow_1;
-
ncol = ncol_1;
-
-
result= (int **)malloc(sizeof(int **) * nrow);
-
-
for (row_loop = 0; row_loop < nrow; row_loop++)
-
{
-
result[row_loop] = (int *)malloc(sizeof(int *) * ncol);
-
}
-
if (result == NULL)
-
{
-
perror("Dynamic Memory Allocation for result Fails");
-
}
-
-
for (row_loop = 0;row_loop < nrow; row_loop++)
-
{
-
for (col_loop = 0; col_loop < ncol; col_loop++)
-
{
-
// *(*(result + row_loop) + col_loop) = (*(*(rowptr + row_loop) + col_loop)) + (*(*(rowptr_1 + row_loop) + col_loop));
-
result[row_loop][col_loop]=rowptr[row_loop][col_loop]+rowptr_1[row_loop][col_loop];
-
}
-
}
-
Display(result,nrow,ncol);
-
-
}
-
-
// ------------------------------------------------------------
-
void Display(int **result, int nrow, int ncol)
-
{
-
int row_loop, col_loop;
-
// Loop for row and column
-
-
for (row_loop = 0; row_loop < nrow; row_loop++)
-
{
-
for (col_loop = 0; col_loop < ncol; col_loop++)
-
{
-
printf("The value of array at [%d][%d] is %d\n", row_loop, col_loop, *(*(result + row_loop) + col_loop) );
-
}
-
}
-
-
}
-
-
// ------------------------------------------------------------
-
-
-
Thanks
-
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,537 network members.
|