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

  • SEARCH
  • Home
  • 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 6911143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:54:50+00:00 2026-05-27T08:54:50+00:00

I am writing a matrix class for a math project, and I have a

  • 0

I am writing a matrix class for a math project, and I have a strange problem where the copy constructor I’ve written fails in one function but succeeds in another- and the circumstances are seemingly identical. I’ve run it through gdb and the copy constructor executes but somehow just doesn’t assign the values after it finishes.

Here is the output from gdb:

(gdb) break main.cpp:20
Breakpoint 1 at 0x8048913: file main.cpp, line 20.
(gdb) run
Starting program: /home/ian/Documents/math471/src/compress 
printing
1 1 1 1 
1 2 3 4 
1 3 5 7 
1 4 7 10 
printing
1 1 1 1 
1 2 3 4 
1 3 5 7 
1 4 7 10 
printing
1 1 1 1 
1 2 3 4 
1 3 5 7 
1 4 7 10 
printing
1 1 1 1 
1 2 3 4 
1 3 5 7 
1 4 7 10 

Breakpoint 1, main (argc=1, argv=0xbffff334) at main.cpp:20
20          tridiagonalize(A);
(gdb) s
tridiagonalize (A=...) at tridiagonalize.cpp:10
10          Matrix result(A);
(gdb) print result
$1 = {transposed = false, height = 3903476, width = 0, data = 0x0}
(gdb) s
Matrix::Matrix (this=0xbffff23c, A=...) at matrix.cpp:176
176         height = A.getHeight();
(gdb) print height
$2 = 0
(gdb) n
177         width = A.getWidth();
(gdb) print height
$3 = 4
(gdb) n
178         data = new double*[height];
(gdb) print width
$4 = 4
(gdb) n
179         for(int i = 0; i < height; i++){
(gdb) break matrix.cpp:185
Breakpoint 2 at 0x80492ba: file matrix.cpp, line 185.
(gdb) c
Continuing.

Breakpoint 2, Matrix::Matrix (this=0xbffff23c, A=...) at matrix.cpp:185
185         transposed = false;
(gdb) n
186 }
(gdb) print transposed
$5 = false
(gdb) print this
$6 = (Matrix * const) 0xbffff23c
(gdb) print this->height
$7 = 4
(gdb) n
tridiagonalize (A=...) at tridiagonalize.cpp:11
11          int n = A.getWidth();
(gdb) print result
$8 = {transposed = false, height = 3903476, width = 0, data = 0x0}
(gdb) 

As you can see, the values in result are the same before the constructor as after.

#include "matrix.h"
#include "tridiagonalize.h"


using namespace std;

void function(Matrix &A);

int main(int argc, char** argv){

        Matrix A(4, 4);
        for(int i = 0; i < 16; i++){
                A.set(i/4, i%4, (i%4)*(i/4)+1);
        }
        function(A);
        Matrix B = A;
        A.print();
        B.print();

        tridiagonalize(A);
        //A.print();

        return 0;
}

void function(Matrix &A){
        Matrix result(A);
        A.print();
        result.print();
}

Matrix tridiagonalize(Matrix &A){

        Matrix result(A);
        int n = A.getWidth();
        cout << "width: " << n << endl;
        cout << "width of result: " << result.getWidth() << endl;
            return result;
}

EDIT: here is the matrix code:

void Matrix::set(int i, int j, double value){
        if(transposed){
                int tmp = i;
                i = j;
                j = tmp;
        }
        if(i < 0 || j < 0 || i >= height || j >= width){
                cout << "trying to set value outside of matrix" << endl;
                return;
        }
        if(height > 0 && width > 0){
                data[i][j] = value;
        }
        return;
}

void Matrix::print() const{
        cout << "printing" << endl;
        if(!transposed){
                for(int i = 0; i < height; i++){
                        for(int j = 0; j < width; j++){
                                cout << data[i][j] << " ";
                        }
                        cout << endl;
                }
        }
        else{
                for(int i = 0; i < width; i++){
                        for(int j = 0; j < height; j++){
                                cout << data[j][i] << " ";
                        }
                        cout << endl;
                }
        }
        return;
}

Matrix::~Matrix(){
        //cout << "deleting Matrix" << endl;
        if(data != NULL){
                for(int i = 0; i < height; i++){
                        //cout << data[i] << endl;
                        delete[] data[i];
                }
                //cout << data << endl;
                delete[] data;
        }
}

double Matrix::get(int i, int j) const {
        if(transposed){
                int tmp = i;
                i = j;
                j = tmp;
        }
        if(data != NULL && i >=0 && j >= 0 && i < height && j < width){
                return data[i][j];
        }
        else{
                cout << "error in get" << endl;
        }
        return -1;
}

int Matrix::getHeight() const {
        if(transposed){
                return width;
        }
        else{
                return height;
        }
}

int Matrix::getWidth() const {
        if(transposed){
                return height;
        }
        else{
                return width;
        }
}



Matrix::Matrix(const Matrix& A){
        height = A.getHeight();
        width = A.getWidth();
        data = new double*[height];
        for(int i = 0; i < height; i++){
                data[i] = new double[width];
                for(int j = 0; j < width; j++){
                        data[i][j] = A.get(i,j);
                }
        }
        transposed = false;
}

Matrix Matrix::operator= (Matrix &B){
        if(data != NULL){
                for(int i = 0; i < height; i++){
                        delete[] data[i];
                }
                delete[] data;
        }
        data = NULL;
        Matrix result(B);
        return result;
}

The tridiagonalize function and the ‘function’ function are set up the same, but function() works and tridiagonalize() doesn’t. Any ideas why this is happening?

EDIT: added the matrix code

  • 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-27T08:54:51+00:00Added an answer on May 27, 2026 at 8:54 am

    Matrix::operator=() isn’t working correctly, it frees the old data array but then doesn’t create a new one with the data that should be assigned.

    It should rather look like this:

    Matrix& Matrix::operator= (const Matrix &B){
       // Don't do anything if B is the same object
       if (&B == this)
          return *this;
    
       // delete data, as in the current version
       if(data != NULL){
          ...
       }
    
       // Create new array of arrays, fill with numbers from B
       data = new double*[B.getHeight()];
       ...
    
       return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a sparse matrix class. I need to have a node class,
I'm writing a class for Matrix arithmetic, and one feature I'm implementing is that
So I was writing a simple generic matrix class and ran into a problem
I have a template class which implements function: template<typename T> class Matrix { ...
For a class project I am writing a simple matrix multiplier in Python. My
I'm writing a sparse matrix class in C++ in which every row and column
I am writing an abstract matrix class (and some concrete subclasses) for use on
I'm writing a custom iterator for a Matrix class, and I want to implement
I'm writing a class to represent a matrix. I want it to look something
I've started writing a class to model a matrix and the compiler gives me

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.