-3

Please look that after memcpy, I am changing the values in source matrix. They are automatically being replaced in destination. Further when I change destination array values they come to source array. Why is this happening? Interestingly even after deleting the destination array by free command the values still remain the source. Please let me know this. Thanks in advance.

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

double *** double3d(long int dim1,long int dim2,long int dim3)
{
    long int i,j,k;
    double ***array;
    array=(double ***)malloc(dim1*sizeof(double **));
    for(i=0;i<dim1;i++)
    {
     array[i]=(double **)malloc(dim2*sizeof(double *));
     for(j=0;j<dim2;j++)
      array[i][j]=(double *)malloc(dim3*sizeof(double ));
    }
    return array;
}// end double3d

void summ(double ***A,double ***B, double ****C)
{
    int i ,j ,k;
    for(i=0;i<10;i++)
        for(j=0;j<5;j++)
            for(k=0;k<5;k++)
                (*C)[i][j][k] = A[i][j][k] + B[i][j][k];
}

void main()
{
    int i,j,k,nx,ny;
    double ***M1, ***M2, ***M3, ***M4,***M5,***M6;
    nx=5;ny=5;
    M1=double3d(10,nx,ny);
    M2=double3d(10,nx,ny);
    M3=double3d(10,nx,ny);
    M4=double3d(5,nx,ny);
    M5=double3d(5,nx,ny);
    M6=(double ***)malloc(10*sizeof(double **));

    for(i=0;i<10;i++)
    {
        for(j=0;j<nx;j++)
            for(k=0;k<ny;k++)
            {
                M1[i][j][k]=i;
                M2[i][j][k]=1;
            }
    }

    // Note random values are in M4 and M5 as they are not initalised
    memcpy(M6,     M4, 5 * sizeof(double **));
    memcpy(M6+5,   M5, 5 * sizeof(double **));

    for(i=0;i<5;i++)
    {
       for(j=0;j<nx;j++)
          for(k=0;k<ny;k++)
          {
               M4[i][j][k]=200;
               M5[i][j][k]=700;
          }
    }

    printf(" printing M6 Memcpy before addition\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%f ",M6[4][j][k]);
       printf("\n");
       for(k=0;k<ny;k++)
           printf("%f ",M6[9][j][k]);
       printf("\n");
    }
    // calling for non memcpy array
    summ(M1,M2,&M3); printf(" Non memcpy output last value : %f \n",M3[9][nx-1][ny-1]);
    // calling for memcpy
    summ(M1,M2,&M6); printf(" memcpy output last value : %f \n",M6[9][nx-1][ny-1]);
    printf(" printing M6 Memcpy for two sets after addtion\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%f ",M6[4][j][k]);
       printf("\n");
    }
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%f ",M6[9][j][k]);
       printf("\n");
    }

    free(M6);// cleared M6

    printf(" printing M4 Memcpy after deleting M6\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%.1f ,%.1f ,%.1f ,%.1f ,%.1f ",M4[0][j][k],M4[1][j][k],M4[2][j][k],M4[3][j][k],M4[4][j][k]);
       printf("\n");
    }

    printf(" printing M5 Memcpy after deleting M6\n");
    for(j=0;j<nx;j++)
    {
       for(k=0;k<ny;k++)
           printf("%.1f ,%.1f ,%.1f ,%.1f ,%.1f ",M5[0][j][k],M5[1][j][k],M5[2][j][k],M5[3][j][k],M5[4][j][k]);
       printf("\n");
    }
 }
zett42
  • 25,437
  • 3
  • 35
  • 72

1 Answers1

2

The "problem" here is that you misunderstand (apparently) what memcpy does.

With

memcpy(M6,     M4, 5 * sizeof(double **));

what you are basically doing is

M6[0] = M4[0];
M6[1] = M4[1];
// etc...

I.e. you are copying the pointers and not what they point to.

When call free(M6) you only free the memory that M6 is pointing to. But the data pointed to by e.g. M6[0] and M4[0] still exists, and is still pointed to by M4[0].

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621