I am a student and I am having trouble understanding Dynamic Memory Allocation and the usage of Double Pointers.
In the above snippet r1 and c1 refer to the number of rows and columns of a matrix A.
My question : What does the malloc function do here? I know malloc() is used to allocate memory during runtime and the argument(r1*(si zeof(int*)) is supposed to the size of the memory allocated. Does the argument mean r1 x size of int (ie) r1 x 2 (where 2 is the size of an integer in the memory)?
And what does the coding inside the for loop do? Why is each element being allocated new space again?
Code:
int **A A=(int **)malloc(r1*(sizeof(int*))); for(i=0;i<r1;i++) A[i]=(int *)malloc(sizeof(int)*c1)
My question : What does the malloc function do here? I know malloc() is used to allocate memory during runtime and the argument(r1*(si zeof(int*)) is supposed to the size of the memory allocated. Does the argument mean r1 x size of int (ie) r1 x 2 (where 2 is the size of an integer in the memory)?
And what does the coding inside the for loop do? Why is each element being allocated new space again?
Comment