0

The code was constructed using the property that the column of A and the row of B must be the same to find the matrix product.

It worked when I multiplied the 2D matrix without using dynamic allocation, but it didn't work after using dynamic allocation with function. So I looked up the address, and the address was different by 30.

int *matrixMulti(int N, int M, int L, int *A, int *B)
{

  int *Mat = (int *)malloc(N * L * sizeof(int));

  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < L; j++)
    {
      *(Mat + i * M + j) = 0;
      for (int k = 0; k < M; k++)
      {
        *(Mat + i * M + j) += A[i * M + k] * B[k * M + j];
      }
      printf("Mat[%d][%d] Mat[i][j] %d Mat address : %p \n", i, j, *(Mat + i * M + j), Mat);
      printf("\n");
    }
  }

  return Mat;
}


int main()
{
  int N, M, L;
  printf("input N M L\n      ");
  scanf("%d %d %d", &N, &M, &L);

  int *A = (int *)malloc(N * M * sizeof(int));
  int *B = (int *)malloc(L * M * sizeof(int));
  int *S = (int *)malloc(N * L * sizeof(int));

  input(N, M, A);
  Array_print(N, M, A);

  input(M, L, B);
  Array_print(M, L, B);

  S = matrixMulti(N, M, L, A, B);

  printf("\n S address %p\n", matrixMulti(N, M, L, A, B)); ////////////////////////// ---> A
  printf("\n S address %p\n", S);                          ////////////////////////// ---> A-30

  // Array_print(N, L, matrixMulti(N, M, L, A, B));

  free(A);
  free(B);
  free(S);
  return 0;
}

At first, when only arrays were passed and received, matrix multiplication was also output normally, but it became strange when changing to pointers.

Output

S address 0x159f04120

S address 0x159f040f0

Expected

S address 0x159f04120

S address 0x159f04120

Full code here

  • 1
    Simply because when you call `matrixMulti` you allocate two distinct chunks of memory – 0___________ Mar 24 '23 at 01:45
  • Please read [this](https://stackoverflow.com/questions/72330113/how-to-copy-data-to-a-pointer-returned-by-malloc) and [this](https://stackoverflow.com/questions/58983444/dynamically-allocating-and-copying-an-array) – n. m. could be an AI Mar 24 '23 at 05:03

1 Answers1

1

this is a simplified version of matrixMulti

int *matrixMulti(int N, int M, int L, int *A, int *B)
{

  int *Mat = (int *)malloc(N * L * sizeof(int));

   ////  do stuff

  return Mat;
}

you see that it allocates memory and returns a pointer to it.

If you call it twice you get two different allocations at different addresses

S = matrixMulti(N, M, L, A, B); // call #1

printf("\n S address %p\n", matrixMulti(N, M, L, A, B));  //call #2

EDIT

Note here

 S = matrixMulti(N, M, L, A, B); // call #1

S now contains the value returned by matrixMulti. Ie Mat

int *matrixMulti(int N, int M, int L, int *A, int *B)
{

  int *Mat = (int *)malloc(N * L * sizeof(int));

   ////  do stuff

  return Mat; <<<< this is what gets loaded into S
}

S does not contain the address of matrixMulti

pm100
  • 48,078
  • 23
  • 82
  • 145
  • Thanks for answering the question:) According to you, shouldn't the address of call #2 and *matrixMulti be the same? But it's different. `printf("Mat[%d][%d] Mat[i][j] %d Mat address : %p \n", i, j, *(Mat + i * M + j), Mat);` – Jeong-uk Park Mar 24 '23 at 01:50
  • @Jeong-ukPark - you are confusing the return value of a function with its address. I will edit my answer – pm100 Mar 24 '23 at 04:29