0

I cannot put my finger on why my program hangs and crashes when I pass my 2d-array, declared on the heap, between functions which accept double pointers.

I am getting a strong feeling that it has to do with the method I have chosen to declare the 2d array. Before I created a function to allocate the array, the program could manipulate the data inside the array when passed to a function.

So here is the allocate function and then the function which it crashes inside:

void matrix_malloc(int **matrix, int m, int n);
void matrix_init(int **matrix, int m, int n);

int main(void)
{
  int **matrix;
  int m(3), n(2);

  matrix_malloc(matrix, m, n);
  matrix_init(matrix, m, n); // runtime error
}

void matrix_malloc(int **matrix, int m, int n)
{ // get heap memory
  int i;
  matrix = new int*[m];
  for(i = 0; i < m; i++)
  {
    matrix[i] = new int[n];
  }
}

void matrix_init(int **matrix, int m, int n)
{ // randomize matrix
  int i, j;
  for(i = 0; i < m; i++)
  {
    for(j = 0; j < n; j++)
    {
      matrix[i][j] = rand() % 10 + 1;
    }
  }
}
1
  • Try passing your matrix pointer to matrix_malloc by reference (int **& matrix)
    – WhozCraig
    Apr 9, 2013 at 6:35

4 Answers 4

2

You have to pass your matrix pointer by reference.

void matrix_malloc(int **matrix, int m, int n)

This accepts a copy of matrix. Which means anything you do in matrix_malloc on matrix will not affect the one in main.

Instead it should be

void matrix_malloc(int **& matrix, int m, int n)
                        ^^^

However I suggest you use vectors instead of raw pointers and allocation. That way you don't need to wory about allocation and deallocation.

void matrix_malloc(vector<vector<int> >& matrix, int m, int n);

// You don't need this anymore.
// void matrix_init(int **matrix, int m, int n);

int main(void)
{
  vector<vector<int> > matrix;
  int m(3), n(2);

  // matrix_malloc(matrix, m, n);

  matrix_init(matrix, m, n); 
}


void matrix_init(vector<vector<int> >& matrix, int m, int n)
{ // randomize matrix
  int i, j;
  for(i = 0; i < m; i++)
  {
    vector<int> row;
    for(j = 0; j < n; j++)
    {
      row.push_back(rand() % 10 + 1);
      // matrix[i][j] = rand() % 10 + 1;
    }
    matrix.push_back(row);
  }
}
2
  • Thank you for the example of vector usage, I need to learn how to use this.
    – Leonardo
    Apr 9, 2013 at 6:47
  • @Leonardo Yes. Definitely. It makes life a whole lot easier :)
    – user995502
    Apr 9, 2013 at 6:49
2
void matrix_malloc(int **&matrix, int m, int n);
void matrix_init(int **matrix, int m, int n);

void matrix_malloc(int **&matrix, int m, int n)
{ // get heap memory
  int i;
  matrix = new int*[m];
  for(i = 0; i < m; i++)
  {
    matrix[i] = new int[n];
  }
}

And should work well. Problem, since after this

matrix = new int*[m];

matrix has new address, but since it's local copy of pointer - main doesn't know about it.

1
  • How would you do this, change the & to another *?
    – Leonardo
    Apr 9, 2013 at 6:37
2

matrix_malloc() needs to take the pointer by reference:

void matrix_malloc(int **&matrix, int m, int n)
                         ^

Without this, the newly-allocated pointer isn't getting propagated back to the caller.

That said, it may be more explicit to return the newly-allocated pointer from the function:

int** matrix_malloc(int m, int n)

Finally, is there a reason you are not using std::vector for this?

1
  • I have not studied how to use this vector yet, I will see about that finally and why it would be a good situation for it.
    – Leonardo
    Apr 9, 2013 at 6:35
1

The allocation of your 2D array is fine. BUT.

  int **matrix;
  int m(3), n(2);

  matrix_malloc(matrix, m, n);

Here, matrix is not going to change - you are copying its value to pass it to the function. What I mean is:

  int **matrix = NULL; // matrix points to null
  int m(3), n(2);

  matrix_malloc(matrix, m, n); // copy the value contained in matrix and give it to the function
  //matrix still points to null

You have multiple solutions :

  • your matrix malloc can return an int**, and you would simply have to write matrix = matrix_malloc(m, n)
  • your matrix malloc could take a pointer to int** (int*** - handle carefully)
  • as mentionned in the other answers, a reference to int**

Here is what a matrix_malloc with an int*** would look like.

//call it as follows:
matrix_malloc(&matrix, m, n);


void matrix_malloc(int ***matrix, int m, int n)
{
  // matrix contains the address of the original variable, so *matrix is the original variable itself.
  int i;
  *matrix = new int*[m];
  for(i = 0; i < m; i++)
  {
    (*matrix)[i] = new int[n];
  }
}
4
  • This is interesting, but you see if I had a pointer to a double pointer and pass by address, it will not work.
    – Leonardo
    Apr 9, 2013 at 6:45
  • You would have to modify your matrix_malloc accordingly of course. I'll edit my answer to illustrate.
    – Nbr44
    Apr 9, 2013 at 6:50
  • Ahh I did not realize I needed to force precedence for the dereference operator, this is why it did not work. This is good to know. Also, it seems like a bad idea to use this method because what if I wanted to again pass by pointer to a function inside that one? Or would that be unlikely as it is a pointer and can copy the address instead.
    – Leonardo
    Apr 9, 2013 at 6:57
  • It will always depend whether you want to modify the pointed data (anything involving [] or *, in that case you can just copy the pointer itself (int**)) or where the pointer is, well, pointing (if there's no [] or * - if you want the modification to be carried over,you'll need to pass the address of the pointer (int***)). Whether or not this method is good is another debate - but in C++ you have plenty of tools to avoid that kind of headache.
    – Nbr44
    Apr 9, 2013 at 7:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.