开发者

Segmentation Fault while using two-dimensional array of pointers

开发者 https://www.devze.com 2023-02-18 10:13 出处:网络
Hey there, this is turning out to really be a tricky one for me. main.cpp #include <stdlib.h> #include <iostream>

Hey there, this is turning out to really be a tricky one for me.

main.cpp

#include <stdlib.h>
#include <iostream>
#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<double> *m = new Matrix<double>(1, 3);
    Matrix<double> *n = new Matrix<double>(3, 1);
    Matrix<double> *mn;
    // Set each row or column
    m->set_row(row1, 0);
    n->set_col(col1, 0);
    std::cout << "Matrix M: \n";
    m->print();
    std::cout << "Matrix N: \n";
    n->print();
    std::cout << "Matrix MN: \n";
    mn = m->mult(n);
    mn->print();
    return (EXIT_SUCCESS);
}

matrix.h

/* 
 * File:   Matrix.h
 * Author: charles
 *
 * Created on March 16, 2011, 12:45 AM
 */
#ifndef _MATRIX_H_
#define _MATRIX_H_

template <typename T>
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 < _rows; ++i){
            _matrix[i] = new T [_cols];
        }
    }

    virtual ~Matrix(){
        // Delete everything
        for(int i = 0; i < _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 < _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 < _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 < _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 < _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 < _rows; ++i){
            for(int j = 0; j < _cols; ++j){
                std::cout << _matrix[i][j] << " ";
            }
            std::cout << "\n";
        }
    }

    T comp_mult(int _r, T* _c){
        T temp;
        for(int i = 0; i < _cols; ++i){
            std::cout << "Comp " << i << "\n";
            if(i == 0) temp = _matrix[_r][i] * _c[i];
            else temp += _matrix[_r][i] * _c[i];
        }
        return temp;
    }

    // Add one matrix to another开发者_Python百科 and return new matrix
    Matrix* add(Matrix* _m){

        // Cannot add matrices if they do not have the same dimensions
        if(!(_rows == _m->get_rows() && _cols == _m->get_cols())){
            std::cout << "Not equal!";
            return NULL;
        }
        else{
            Matrix<T>* _t = new Matrix<T>(_rows, _cols);
            for(int i = 0; i < _rows; ++i){
                for(int j = 0; j < _cols; ++j){
                    _t->set_elem(_matrix[i][j] + _m->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->get_rows()){
            return NULL;
        }
        else{
            Matrix<T>* _t = new Matrix<T>(_rows, _m->get_cols());
            // Print out dimensions
            // std::cout << "Dimensions: r = " << _t->get_rows() << " c = " << _t->get_cols() << "\n";
            // Each row of _t
            for(int i = 0; i < _t->get_rows(); ++i){
                // Each value in each row of _t
                for(int j = 0; j < _t->get_cols(); ++j){
                    T temp; // Temp variable to hold value for _t[i][j]
                    // Each row in _matrix
                    std::cout << "Loop i:" << i << "\n";
                    temp = this->comp_mult(i, _m->get_col(j));
                    std::cout << "loop j: " << j << "\n";
                    std::cout << "TEMP = " << temp << "\n";
                    _t->set_elem(temp, i, j); // this is where i segfault
                }
            }
        }
    }

    // Multiply entire matrix by number and return new matrix
    Matrix* scalar(T _n){
        Matrix<T>* _t = new Matrix<T>(_rows, _cols);
        for(int i = 0; i < _rows; ++i){
            for(int j = 0; j < _cols; ++j){
                _t->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_ */

Sample Output

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

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?


You forgot to return _t from mult() - The debugger likely gives you an incorrect location for the crash you get when calling mn->print()

Also, get_col and get_row leaks memory, and you need to make a copy constructor and an assignment operator.


_t->set_elem(temp, i, j); // this is where i segfault

My sixth sense tells me that you're going out of index. Check out your index values! Are they within limit?

Implement set_elem() as:

void set_elem(T _t, int _r, int _c){
     if ( _ r >= _rows || _c >= _cols ) 
           throw std::out_of_range("index out of range");
    _matrix[_r][_c] = _t;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消