![]() |
Dynamic Memory Allocation for Matrix(2D Array)
Hello all expert C programmer especially shabbir,
i have a question which bring my emotion from happy to unhappy. My program is a use of techniques dynamic memory allocation to alocate the memory for 2D array. I have successfully allocating the memory for 2D but when i try to add two matrix, the program just display an error message which force my program terminated. I know this situation is related to run time error. I try to debug and catch the errors. Unfortunately, my effort doesn't bring any desires results. I hope that someone which kind enough to help me in this problem. Below is my code: Code:
/* Best Method to allocate memory for 2D Array because it's Thanks for your help. Your help is greatly appreciated by me and others. |
Re: Dynamic Memory Allocation for Matrix(2D Array)
nrow_1 needs to be ncol_1
Code:
for (row_loop=0;row_loop<nrow_1;row_loop++)Code:
void Add(int **rowptr, int **rowptr_1, int nrow, |
Re: Dynamic Memory Allocation for Matrix(2D Array)
Hey man, giving a first look to your code it looked as if you are well versed i programming, but after thoroughly going i thought either you are not clear with mem managament concepts of have just copied code.
i have made certain change that ececute code correctly. Many Confusing comments were given... I am still not able to figure the need of them... Also many unnecessary inputs were asked.... and theres no ned to take symmetric matrix, any matrix with resultant matrix equal to size of largest matrix would solve the problem... Go through changes and understand.. Also try to use code that others should feel easy to understand.... Code:
/* Best Method to allocate memory for 2D Array because it's |
Re: Dynamic Memory Allocation for Matrix(2D Array)
I font understand whether you guys are actually famaliar with synataxes.
while allocating you should know that malloc returns null pointer so that has to be typecasted. n you people straightway write mlloc(...); Try not to repeat. Any newbie reading this would get confused and get ample errors to get his head scratched... |
Re: Dynamic Memory Allocation for Matrix(2D Array)
Malloc only returns a NULL pointer if it fails. Otherwise, in C (not C++), it returns a void pointer which needs to be cast.
|
Re: Dynamic Memory Allocation for Matrix(2D Array)
Quote:
|
Re: Dynamic Memory Allocation for Matrix(2D Array)
For the older compiler(K&R), you need cast the void pointer to the type data you pointed to but for modern compiler, you doesn't need to cast the void pointer to the type of data the pointing to.
I don't know whether this is true. I just guess. I abg your pardon if my opinion make you angry. Thanks for all replies. Your help and replies is greatly appreciated by me and others. |
Re: Dynamic Memory Allocation for Matrix(2D Array)
Sorry shabbir, i won't do that again. Thank for your remind.
|
Re: Dynamic Memory Allocation for Matrix(2D Array)
Hello all, i have successfully run the program but i not really understand two statement in the program.
The two statement is as below; 1. rowptr = (int **)malloc(sizeof(int **) * nrow); Allocate nrow of array of pointer - where if nrow = 5. The above statement is same as int *rowptr[5]; -- I don't know whether is it correct or not. 2. for (row_loop=0;row_loop<nrow;row_loop++) { rowptr[row_loop] = (int *)malloc(sizeof(int *) * ncol); } For each rowptr, allocate int * pointer pointing to ncol. I don't know whether my understanding is correct or not. Please confirm with me whether my understanding is correct or not. Thanks for your help. Your help is greatly appreciated by me and others. I don't know how to return the help you all have give. By the way, i just know say thanks to you all. Without you all, i think i not able to solve this program. This program is not assignment nor project. I just practiced it on my own. Thanks. Thanks. Thanks. Thanks. Thanks. Thanks. Thanks. Thanks. |
Re: Dynamic Memory Allocation for Matrix(2D Array)
Ya thanks DaWei, i meant to say void pointer, but was just a mistake that i wrote null pointer. Thanks for correction..
|
| All times are GMT +5.5. The time now is 04:27. |