Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4090738
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:16:36+00:00 2026-05-20T19:16:36+00:00

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

  • 0

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 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:16:37+00:00Added an answer on May 20, 2026 at 7:16 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.