Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation Fault while using two-dimensional array of pointers
    text
    copied!<p>Hey there, this is turning out to really be a tricky one for me.</p> <p><em><strong>main.cpp</em></strong></p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;iostream&gt; #include "Matrix.h" int main(int argc, char** argv) { // Dummy matrix double row1[3] = {3, -1, -2}; double col1[3] = {4, 3, 1}; // Initialize matrix with 4 x 2 dimensions Matrix&lt;double&gt; *m = new Matrix&lt;double&gt;(1, 3); Matrix&lt;double&gt; *n = new Matrix&lt;double&gt;(3, 1); Matrix&lt;double&gt; *mn; // Set each row or column m-&gt;set_row(row1, 0); n-&gt;set_col(col1, 0); std::cout &lt;&lt; "Matrix M: \n"; m-&gt;print(); std::cout &lt;&lt; "Matrix N: \n"; n-&gt;print(); std::cout &lt;&lt; "Matrix MN: \n"; mn = m-&gt;mult(n); mn-&gt;print(); return (EXIT_SUCCESS); } </code></pre> <p><em><strong>matrix.h</em></strong></p> <pre><code>/* * File: Matrix.h * Author: charles * * Created on March 16, 2011, 12:45 AM */ #ifndef _MATRIX_H_ #define _MATRIX_H_ template &lt;typename T&gt; class Matrix { public: Matrix(int _r, int _c){ // Set row and column size, then initialize matrix _rows = _r; _cols = _c; _matrix = new T * [_rows]; for(int i = 0; i &lt; _rows; ++i){ _matrix[i] = new T [_cols]; } } virtual ~Matrix(){ // Delete everything for(int i = 0; i &lt; _cols; ++i){ delete[] _matrix[i]; } delete[] _matrix; } // Get number of rows unsigned int get_rows(){ return _rows; } // Get number of columns unsigned int get_cols(){ return _cols; } // Returns the row from _matrix as an array T* get_row(int _r){ T* _t = new T [_cols]; for(int i = 0; i &lt; _cols; ++i){ _t[i] = _matrix[_r][i]; } return _t; } // Returns the column from _matrix as an array T* get_col(int _c){ T* _t = new T [_rows]; for(int i = 0; i &lt; _rows; ++i){ _t[i] = _matrix[i][_c]; } return _t; } T get_elem(int _r, int _c){ return _matrix[_r][_c]; } // Set a specific row with an array of type T void set_row( T _t[], int _r ){ for(int i = 0; i &lt; _cols; ++i){ _matrix[_r][i] = _t[i]; } } // Set a specific column with an array of type T void set_col( T _t[], int _c){ for(int i = 0; i &lt; _rows; ++i){ _matrix[i][_c] = _t[i]; } } // Set a specific index in the matrix with value T void set_elem(T _t, int _r, int _c){ _matrix[_r][_c] = _t; } // Test to see if matrix is square bool is_square(){ return (_rows == _cols); } // Print contents of matrix for debugging purposes void print(){ for(int i = 0; i &lt; _rows; ++i){ for(int j = 0; j &lt; _cols; ++j){ std::cout &lt;&lt; _matrix[i][j] &lt;&lt; " "; } std::cout &lt;&lt; "\n"; } } T comp_mult(int _r, T* _c){ T temp; for(int i = 0; i &lt; _cols; ++i){ std::cout &lt;&lt; "Comp " &lt;&lt; i &lt;&lt; "\n"; if(i == 0) temp = _matrix[_r][i] * _c[i]; else temp += _matrix[_r][i] * _c[i]; } return temp; } // Add one matrix to another and return new matrix Matrix* add(Matrix* _m){ // Cannot add matrices if they do not have the same dimensions if(!(_rows == _m-&gt;get_rows() &amp;&amp; _cols == _m-&gt;get_cols())){ std::cout &lt;&lt; "Not equal!"; return NULL; } else{ Matrix&lt;T&gt;* _t = new Matrix&lt;T&gt;(_rows, _cols); for(int i = 0; i &lt; _rows; ++i){ for(int j = 0; j &lt; _cols; ++j){ _t-&gt;set_elem(_matrix[i][j] + _m-&gt;get_elem(i, j), i, j); } } return _t; } } // Multiply two matrices and return new matrix Matrix* mult(Matrix* _m){ // If dimensions are not compatible return NULL if(_cols != _m-&gt;get_rows()){ return NULL; } else{ Matrix&lt;T&gt;* _t = new Matrix&lt;T&gt;(_rows, _m-&gt;get_cols()); // Print out dimensions // std::cout &lt;&lt; "Dimensions: r = " &lt;&lt; _t-&gt;get_rows() &lt;&lt; " c = " &lt;&lt; _t-&gt;get_cols() &lt;&lt; "\n"; // Each row of _t for(int i = 0; i &lt; _t-&gt;get_rows(); ++i){ // Each value in each row of _t for(int j = 0; j &lt; _t-&gt;get_cols(); ++j){ T temp; // Temp variable to hold value for _t[i][j] // Each row in _matrix std::cout &lt;&lt; "Loop i:" &lt;&lt; i &lt;&lt; "\n"; temp = this-&gt;comp_mult(i, _m-&gt;get_col(j)); std::cout &lt;&lt; "loop j: " &lt;&lt; j &lt;&lt; "\n"; std::cout &lt;&lt; "TEMP = " &lt;&lt; temp &lt;&lt; "\n"; _t-&gt;set_elem(temp, i, j); // this is where i segfault } } } } // Multiply entire matrix by number and return new matrix Matrix* scalar(T _n){ Matrix&lt;T&gt;* _t = new Matrix&lt;T&gt;(_rows, _cols); for(int i = 0; i &lt; _rows; ++i){ for(int j = 0; j &lt; _cols; ++j){ _t-&gt;set_elem(_matrix[i][j] * _n, i, j); } } return _t; } private: unsigned int _rows; // Number of rows unsigned int _cols; // Number of columns T** _matrix; // Actual matrix data }; #endif /* _MATRIX_H_ */ </code></pre> <p><em><strong>Sample Output</em></strong></p> <pre><code>Matrix M: 3 -1 -2 Matrix N: 4 3 1 Matrix MN: Loop i:0 Comp 0 Comp 1 Comp 2 loop j: 0 TEMP = 7 Segmentation fault </code></pre> <p>So the problem I'm having is that I am getting a segmentation fault when trying to assign a new matrix a value. It does not make sense to me because I have initialized the new matrix that I am trying to assign a value, I know its size, I'm not trying to access memory outside the matrix, and I know that my temp variable has a value. Anyone with a suggestion? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload