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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:52:46+00:00 2026-06-15T07:52:46+00:00

Annoyance over C++’s requirement to pass a dimension in a 2-d array got me

  • 0

Annoyance over C++’s requirement to pass a dimension in a 2-d array got me working on a templated Matrix class. I’ve been coding in C# for a bit, so I’m sure I’m a little rusty here.

Issue is, I get a heap exception as soon as I hit the destructor, which is trying to delete the 2-d array.

Any help gratefully accepted!

template <typename T>
class Matrix {
public:
    Matrix(int m, int n) : nRows(m), nCols(n) { 
        pMatrix = new T * [nRows]; 
        for (int i = 0; i < nCols; i++) {
            pMatrix[i] = new T[nCols];
        }
    }
    ~Matrix() { 
        if (pMatrix != NULL) { 
            for (int i = 0; i < nRows; i++) { delete[] pMatrix[i]; }
            delete[] pMatrix;
        }
    }
    T ** GetMatrix() const { return pMatrix; }
    T * Row(int i) const { return pMatrix[i]; }
    inline T Cell(int row, int col) const { return pMatrix[row][col]; }
    inline int GetNRows() const { return nRows; }
    inline int GetNCols() const { return nCols; }
private:
    int nRows, nCols;
    T ** pMatrix;
};
  • 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-15T07:52:47+00:00Added an answer on June 15, 2026 at 7:52 am

    Concerning the bug, @CodeChords_man explained it right. I have notes on implementation. I recommend to look through this wonderful FAQ post.

    You should not use dynamic memory allocation unless you are 100% sure that

    1. You really need it
    2. You know how to implement it

    I don’t know of the first, and how the performance is crucial for you. But as for the second, you at least violated the rule of three. You class is very unsafe to use. If you copy it, the memory buffer will then be double-deleted.

    You should not afraid to used STL containers, they are fast and optimized. At least the std::vector, it is as fast as the raw pointer in many scenarios. You can rewrite you class using std::vector as follows:

    template <typename T>
    class Matrix {
    public:
      typedef std::vector<T> MatrixRow;
      typedef std::vector<MatrixRow> MatrixBody;
    
      Matrix(int m, int n) : nRows(m), nCols(n), _body(m, MatrixRow(n)) {}
    
      const MatrixBody& GetMatrix() const { return _body; }
      const MatrixRow& GetRow(int i) const { return _body[i]; }
    
      inline T Cell(int row, int col) const { return _body[row][col]; }
      inline int GetNRows() const { return nRows; }
      inline int GetNCols() const { return nCols; }
    private:
      int nRows, nCols;
      MatrixBody _body;
    };
    

    Since this class is not using dynamic memory allocation, it is safe to copy and assign. You also don’t need to explicitly store nRows and nCols in this case; you can use _body.size() and _body[0].size() instead.

    Concerning underlying vector of vectors, it is dereferenced using the same [i][j] construction. It is easily iterated with begin() and end(). And if you absolutely need to use the raw pointer in some routine, you can always access it with &row[0].

    The only possible difficulty is that you cannot easily convert MatrixBody to T**. But think it twice, maybe you don’t really need to use T** at all.

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

Sidebar

Related Questions

I've started working a little bit with lift+scala+mongorecord but I found a small annoyance
G'day there. I've currently got a bit of an annoyance more than anything. The
Windows 7 annoyance: the jump lists are fine for hovering over the taskbar items,
Ok, this has been an on-going annoyance, so naturally I thought to bring it
An annoyance that I sometimes come across with SVN is the working copy getting
one of my favorite annoyance when coding in C++ is declaring some static variable
I have a slight annoyance with my heroku push/deploy process, which otherwise has been
I’ve run into a little bit of an annoyance whilst trying to rebind a
This has been an annoyance I've come across a few times so I thought
Im working on a big MVC3 web application and have an annoyance regarding the

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.