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 8871967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:06:43+00:00 2026-06-14T18:06:43+00:00

I have a matrix class and, with the following constructor: template<class T> Matrix<T>::Matrix(unsigned rows,

  • 0

I have a matrix class and, with the following constructor:

template<class T>
Matrix<T>::Matrix(unsigned rows, unsigned cols) :
        rows(rows), cols(cols) {
    index = 0;
    data_ = new T[rows * cols];
}

template<class T>
Matrix<T>::~Matrix() {
    delete[] data_;
}

When I’m calculating the inverse of the matrix, I would like to free the memory of the
temporary variable called a:

template<class T>
Matrix<T> Matrix<T>::inverse() {
    unsigned i, j, k;
    Matrix<T> a(2 * rows, 2 * rows);        
    ....
    return tmp;
}

I thought that this variable would be destroyed in the end of the function, but when I test:

for (int i = 0; i < 3; i++) {   
        Matrix<double> m(5, 5);
        m << 5, 2, 4, 5, 6, 1, 3, 1, 2, 5, 2, 5, 2, 7, 2, 9, 2, 1, 0.1, 0.43, 1, 0, 0, 0, 1;
        m.inverse();
        std::cout << m << std::endl;
    }

In the first loop the a is initialized with zeros, but the next step the initial values of the a is the previous values, so a(k+1)=a_endvalues(k). Why is it like this?

  • 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-06-14T18:06:44+00:00Added an answer on June 14, 2026 at 6:06 pm

    The problem is that you are not initializing the elements of your dynamically allocated array in the constructor. To ensure that the array has default constructed or zero-initialized elements, you need to do this in your constructor:

    template<class T>
    Matrix<T>::Matrix(unsigned rows, unsigned cols) :
            rows(rows), cols(cols) {
        index = 0;
        data_ = new T[rows * cols]();
                    //             ^ HERE!
    }
    

    But as has been pointed out in comments, you could make your life easier by using an std::vector<T>:

    template<class T>
    Matrix<T>::Matrix(unsigned rows, unsigned cols) :
            rows(rows), cols(cols), data_(rows*cols) 
    { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class: template <typename T> class matrix { private: int _n;
I have a class template<size_t N, size_t M> class Matrix { // .... };
I have the following template structure: template <typename scalar_type> struct postc_params{ scalar_type delta; unsigned
So I have a generic Matrix class that I created which has the following
I have a matrix class like below: template <size_t M, size_t N, typename T>
I have a code base, in which for Matrix class, these two definitions are
I have a class that wraps logic for matrix management. Meanwhile I have a
I have a matrix of data (the columns represent time, and the rows spectrum
I have a simple matrix class which has a 2d integer pointer field in
I am trying to create a Matrix class using c++. So far, I have

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.