1

I am trying to make a function that prints a 2d array. Here is my code:

void print_matrix(float*** mat, int dim01, int dim02){
  for(int i=0; i<dim01; i++){
    for(int j=0; j<dim02; j++){
      cout<<*mat[i][j]<<" ";
    }
    cout<<endl;
  }
}

I also tried an alternative code as this threw an error.

[1] 2999 Segmentation Fault

The second function is :

void print_matrix(float** mat, int dim01, int dim02){
  for(int i=0; i<dim01; i++){
    for(int j=0; j<dim02; j++){
      cout<<mat[i][j]<<" ";
    }
    cout<<endl;
  }
}

This also threw a segmentation fault with 3617 as instead of 2999.

Just to make things clear. In the first function I passed array's address but in 2nd I just passed the array.

What is the error I am making here. I know that segmentation fault is because of memory management error but I can't find it!

the whole code goes like this :

#include<iostream>
using namespace std;

//some useful functions
float** cofactor(float**, int, int, int);
void input_matrix(float**, int, int);
void print_matrix(float**, int, int);

//main
int main(){
  int size;
  int a,b;
  float** arr01; float** arr02;

  cout<<"Size of matrix : ";
  cin>>size;

  input_matrix(arr01,size,size);
  cout<<endl<<"Input Successful..."<<endl;
  /*
  cout<<"Enter the element to find the cofactor [i,j] : ";
  cin>>a>>b;

  cofactor(arr01,size,a,b);
*/
  print_matrix(arr01,size,size);

  return 0;
}

//definitions
void input_matrix(float** mat, int dim01, int dim02){
  mat = new float*[dim01];
  cout<<"Enter the matrix : "<<endl;
  for(int i=0; i<dim01; i++){
    mat[i]=new float[dim02];
    for(int j=0; j<dim02; j++){
      cin>>mat[i][j];
    }
  }
}

void print_matrix(float** mat, int dim01, int dim02){

  for(int i=0; i<dim01; i++){
    for(int j=0; j<dim02; j++){
      cout<<mat[i][j]<<" ";
    }
    cout<<endl;
  }

}

Thanks in advance!

2 Answers2

1

For a 2D array, you need float** but you still need to create the array.

const int dim1=100;
const int dim2=100;

float **matrix = new float*[dim1];

for(int i=0;i<dim1;i++)
   matrix[i] = new float[dim2];

//now you can add elements to your 2D array and print them once the elements
//have been added (print_matrix (print_matrix(matrix, dim1,dim2); )

//when done delete the array
 for(int i=0;i<dim1;i++)
    delete [] matrix[i];
 delete [] matrix;

You can also use std::vector<std::vector<float>> , which is preferred, unless performance is an issue (you have a large array).

dsp_user
  • 2,061
  • 2
  • 16
  • 23
  • dim01 and dim02 is a variable. I don't want to consume extra memory. – brightprogrammer Jan 31 '19 at 07:56
  • You're passing an uninitialized array to input_matrix (the original pointer can't be changed in the callee). You should create the array in `main` and change input_matrix to input_matrix(int dim01, int dim2); so that input_matrix just populates the array or alternatively have input_matrix both create and return the array `float** input_matrix(int dim01, int dim02) { … return matrix; }` – dsp_user Jan 31 '19 at 09:00
1

The problem with the code you show now, is that arguments are by default passed by value. Which means the value is copied into the local argument variable of the function. When you modify the copy (for example by assigning to it) then only the copy is changed and not the original.

Now if we look at your input_matrix function, and its declaration:

void input_matrix(float** mat, int dim01, int dim02);

and how it's called:

input_matrix(arr01,size,size);

we can clearly see this problem. Any changes you make to mat inside the function is local to that function only.

There are two solutions:

  1. Either not pass mat at all and have the function return the "array":

    float** input_matrix(int dim01, int dim02);
    
  2. Or pass the argument mat by reference instead:

    void input_matrix(float**& mat, int dim01, int dim02);
    
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621