Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this variable point with regard to malloc?

Tags:

c

pointers

currently i'm learning about pointers.

   mat3 = (double***) malloc(m*sizeof(double**));

This statement indicates that mat3 points to the first array of the two arrays that contain double**.

mat3[0] = (double**) malloc(m*n*sizeof(double*));

I made a small figure to show how I see this. I don't understand why the line with the comment is wrong. In my opinion the same happens as the one above.

   for (i=0; i<m; i++)
   {
      mat3[i] = mat3[0] + i*n;
      for(j=0; j<n; j++)
      {
         mat3[i][j] = mat3[i][0] + j*p; // this goes wrong
      }
   }

In the first loop the adress of the first ** array taken and each iteration 3 is added. The figure also shows that there are 3 * arrays, so that looks OK.

In the second loop the adress of the first * array is taken and each iteration 4 is added. That also looks okay since each * array contains 4 places to store a double. So 4 memory places needs to be skipped.

There is something wrong, but I don't see it. Can I get feedback?

enter image description here

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

int main()
{
   double*** mat3;
   int m,n,p;
   int i,j,k;
   
   m=2; n=3; p=4;

   printf("This program causes a coredump, unless you fix the error in it !\n");
   
   mat3 = (double***) malloc(m*sizeof(double**));
   if (mat3 == NULL)
      exit(1);
   
   mat3[0] = (double**) malloc(m*n*sizeof(double*));
   if (mat3[0] == NULL)
      exit(1);
   
   mat3[0][0] = (double*) malloc(m*n*p*sizeof(double));
   if (mat3[0][0] == NULL)
      exit(1);
   
   for (i=0; i<m; i++)
   {
      mat3[i] = mat3[0] + i*n;
      for(j=0; j<n; j++)
      {
         mat3[i][j] = mat3[i][0] + j*p; // this goes wrong
      }
   }
   
   for(i=0;i<m;i++)
      for(j=0;j<n;j++)
         for(k=0;k<p;k++)
             mat3[i][j][k] = i + 10*j + 100*k;
   
   for(i=0;i<m;i++)
      for(j=0;j<n;j++)
         for(k=0;k<p;k++)
             printf("mat3[%d][%d][%d] = %lf\n",i,j,k,mat3[i][j][k]);
   
   exit(0);
}
like image 853
Tim Avatar asked Feb 23 '26 16:02

Tim


1 Answers

Below is a diagram of what you are trying to create:

enter image description here

The green arrows show the 3 pointers that was initialized using the malloc calls.

The red arrows show the 6 pointers that has not been initialized at this point.

So the code after the 3 malloc must initialize all the red pointers before using them. And your code doesn't do that.

The line mat3[i] = mat3[0] + i*n; will initialize mat3[1].

Then the line mat3[i][j] = mat3[i][0] + j*p; uses mat3[0][0] and mat3[1][0] on the rigth hand side. Use of mat3[0][0] is fine as it has been initialized but use of mat3[1][0] is bad as it's uninitialized.

Change

mat3[i][j] = mat3[i][0] + j*p;

to

mat3[i][j] = mat3[0][0] + i*n*p + j*p;
like image 82
Support Ukraine Avatar answered Feb 26 '26 05:02

Support Ukraine