0

I am able to declare in a good way two matrices A and B.

But, when using the memcpy (to copy B from A), B gives me arrays of 0s.

How can I do? Is my code correct for using memcpy?

int r = 10, c = 10, i, j;

int (*MatrixA)[r];
MatrixA=malloc(c * sizeof(*MatrixA));
int (*MatrixB)[r];
MatrixB=malloc(c * sizeof(*MatrixB));

memcpy(MatrixB,MatrixA,c * sizeof(MatrixA));


for(i=1;i<r+1;i++)
{
     for (j = 1; j < c+1; j++)
    {
        MatrixA[i][j]=j;
         printf("A[%d][%d]= %d\t",i,j,MatrixA[i][j]);
    }
    printf("\n");
}
printf("\n");printf("\n");printf("\n");printf("\n");printf("\n");

for(i=1;i<r+1;i++)
{
     for (j = 1; j < c+1; j++)
    {
        printf("B[%d][%d]= %d\t",i,j,MatrixB[i][j]);
    }
    printf("\n");
}
9
  • Well, have you initialized the mallocated space at all? Commented Oct 3, 2015 at 12:31
  • 3
    memcpy(MatrixB,MatrixA,c * sizeof(MatrixA)); --> memcpy(MatrixB,MatrixA,c * sizeof(*MatrixA)); and move to after value set to MatrixA. also index is out of bounds. because 0 origin. Commented Oct 3, 2015 at 12:34
  • I think it' s not a fact of malloc because the A matrix is well declared and the output is well printed. Commented Oct 3, 2015 at 12:34
  • Yes but you initialize MatrixA after copying its (undefined) contents to MatrixB. Also, indices start at 0. Commented Oct 3, 2015 at 12:36
  • 1
    see SAMPLE Commented Oct 3, 2015 at 12:49

2 Answers 2

3

You copied contents before initializing MatrixA .And also you access index out of bound (r+1 evaluates 11 which is out of bound) causing UB. Do this instead -

for(i=0;i<r;i++)            //  i starts from 0 
{
    for (j =0; j < c; j++)         // j from 0
  {
       MatrixA[i][j]=j;
       printf("A[%d][%d]= %d\t",i,j,MatrixA[i][j]);
  }
   printf("\n");
}

memcpy(MatrixB,MatrixA,c * sizeof(*MatrixA));   // copy after setting MatrixA

for(i=0;i<r;i++)              // similarly indexing starts with 0
{
    for (j =0; j < c; j++)
  {
    printf("B[%d][%d]= %d\t",i,j,MatrixB[i][j]);
  }
  printf("\n");
}
Sign up to request clarification or add additional context in comments.

Comments

-2

Is my code correct for using memcpy?

No, your code is wrong, but that's less of a memcpy problem. You're simply doing C arrays wrong.

int r = 10, c = 10, i, j;

int (*MatrixA)[r];
MatrixA=malloc(c * sizeof(*MatrixA));

Ok, MatrixA is now a pointer to a 10-element array of integers right? So the compiler reserves memory for ten ints; however, in the malloc line, you overwrite that with a pointer to a memory region of ten times the size of a single integer. A code analysis tool will tell you that you've built a memory leak.

These mistakes continue throughout your code; you will have to understand the difference between statically allocated C arrays and dynamic allocation using malloc.

7 Comments

No , I don't think so .There is need to allocate memory to it because MatrixA is just a pointer and uninitialized.
@Marcus Muller, I would like to use the memcpy with dynamic arrays as my code listed above. Can you give me at least what is the right instruction to write with memcpy?
You're doing dynamic arrays wrong, so no, I won't help you shoot your own foot.
@ameyCU: you're right regarding the uninitializedness, but it's already allocated, so malloc is wrong here, definitely.
"MatrixA is now a 10-element array of int pointers, right?" No. You are thinking of int *MatrixA[r];. int (*MatrixA)[r]; is a pointer to an array of r ints.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.