0

So I have the following function. The function takes a target int ** double pointer and copies a source array to it.

void copyMatrix(int ** target, int m, int n, int source[m][n])
{
  int i,j;

  target = (int **) malloc(m * sizeof(int *));
  for (i = 0; i < m; i++)
    target[i] = (int *)malloc(n * sizeof(int));

  for(i=0; i < m; i++){
    for(j=0; j < n; j++){
     target[i][j] = source[i][j];
    }
  }
  printf("%d ", target[i][j]);
}

When i call printf after the for loops, i get a segfault, but if i call printf inside the for loops, it prints target[i][j] correctly. Why is this? I'm tearing my hair out over this...

6
  • 2
    What do you expect the value of i and j to be outside the loop? Commented Nov 21, 2016 at 5:21
  • 4
    You'll need to pass the pointer to your **target, so it becomes ***target. And then use *target = ... when allocating. You're currently trying to modify the (double) pointer itself, not the value it points to. Commented Nov 21, 2016 at 5:22
  • 1
    Also look into stackoverflow.com/a/7307699/1746118 Commented Nov 21, 2016 at 5:24
  • Though you should probably avoid possing triple dereferenced values. You can do it once as an excercise. Consider allocating target outside the copyMatrix function, or return it instead (and don't pass it). Since you have to free it outside the function as it's currently written, you better make copyMatrix behave like malloc and friends. Commented Nov 21, 2016 at 5:25
  • Thanks for all the comments everyone. i think i learned more in about thirty seconds than I ever have about pointers Commented Nov 21, 2016 at 5:31

2 Answers 2

1

After the loops, i == m and j == n. They both point 1 item past the max. Arrays in C are zero-indexed, so accessing target[n][m] of an array with size [n][m] will give an out-of-bounds access.

Sign up to request clarification or add additional context in comments.

Comments

0

I am not clear as to why do you want to create an array of pointers first. With this approach, you end up having a problem of modifying the double pointer itself which will not reflect in the caller. You can simply alloc an array directly to store the input 2D array.

Your code:

void copyMatrix(int ** target, int m, int n, int source[m][n])
{
  int i,j;

  target = (int **) malloc(m * sizeof(int *));
  for (i = 0; i < m; i++)
    target[i] = (int *)malloc(n * sizeof(int));

  for(i=0; i < m; i++){
    for(j=0; j < n; j++){
     target[i][j] = source[i][j];
    }
  }
  printf("%d ", target[i][j]);
}

Modified Code:

void copyMatrix(int ** target, int m, int n, int source[m][n])
{
  int i,j;

  *target = (int *) malloc(sizeof(int) * (m * n));


  for(i=0; i < m; i++){
    for(j=0; j < n; j++){
     (*target)[i][j] = source[i][j];
    }
  }
  printf("%d ", (*target)[i][j]);
}

Comments

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.