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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:57:52+00:00 2026-05-31T21:57:52+00:00

So I have a class called MatrixMxN, in the constructor it has parameters row,

  • 0

So I have a class called MatrixMxN, in the constructor it has parameters row, column. I am attempting to allocate memory for a 2D array with dimensions row, column, although I am getting a problem with doing this..
(I am overriding the parenthesis operator to assign values to each entry)

{
    MatrixMxN coord(4, 1);
    coord(0, 0) = 1.0;
    coord(0, 1) = 1.0;
    coord(0, 2) = 1.0;
    coord(0, 3) = 1.0;
}

The problem I am facing seems to be when the deconstructor is called and I receive the error:-

Windows has triggered a breakpoint in MatrixTest.exe.
This may be due to a corruption of the heap, which indicates a bug in MatrixTest.exe or any of the DLLs it has loaded.

The snippet from my matrix class is as follows;

typedef float* floatPtr;
class MatrixMxN {
private:
    float** entry;
    int rows;
    int cols;

public:
    MatrixMxN(int r, int c) {
        rows = r;
        cols = c;

        //Create a matrix
        if(rows > 0 && cols > 0) {
            //Declare an array of pointers
            entry = new floatPtr[rows];

            //Declare each array
            for(int i=0; i<rows; i++) {
                entry[i] = new float[cols];
            }

            this->empty();
        }
    }
    ~MatrixMxN() {
        //Free memory
        for(int i=0; i<rows; i++) {
            delete[] entry[i];
        }

        //Free pointers array
        delete[] entry;
    }

    void empty() {
        for(int i=0; i<rows; i++) {
            for(int j=0; j<cols; j++) {
                entry[i][j] = 0;
            }
        }
    }

    // Assignment operator
    void operator=(MatrixMxN& other) {
        //Check they are the same size
        assert(rows == other.rows && cols == other.cols);

        //Copy
        for(int i=0; i<rows; i++) {
            for(int j=0; j<cols; j++) {
                entry[i][j] = other(i, j);
            }
        }
    }
    float& operator()(const int irow, const int icol) {
        //Check they are not out of bounds
        assert ( (irow >= 0 && irow < rows) ||  (icol >= 0 && icol < cols) );

        return entry[irow][icol];
    }
...

The part which is tripping up an error is in the deconstructor inside the loop;

    //Free memory
    for(int i=0; i<rows; i++) {
        delete[] entry[i];
    }

The dbgheap.c file throws the error on the first attempt to delete[] entry[i] where i =0. Although when printing the matrix it works fine as intended but there appears to be an error here. Hopefully I have provided enough information here, thanks.

Edit1: Included assignment operator overload
Edit2: Included () overload

Answer:
The problem was I was entering the values in a transpose manner rather than how I had it. The memory was being corrupted here, thanks for all the help.

  • 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-31T21:57:54+00:00Added an answer on May 31, 2026 at 9:57 pm

    This may not be the cause but there is no copy constructor or assignment operator defined for the class MatrixMxN: either define them or make the object non-copyable by declaring them private.

    While not the problem in this example, delete[] will be invoked on an uninitialized pointers in the destructor if rows > 0 but cols <= 0. In the constructor, entry is only initalized if rows > 0 && cols > 0. If rows > 0 but cols <= 0 the following for loop in the destructor still invokes delete[] entry[i];:

    for(int i=0; i<rows; i++) {
        delete[] entry[i]; // entry unitialized
    }
    

    followed by:

    delete[] entry; // entry unitialized
    

    EDIT:

    This assert in operator() is incorrect:

    assert ( (irow >= 0 && irow < rows) ||  (icol >= 0 && icol < cols) );
    

    it should be using &&:

    assert ( (irow >= 0 && irow < rows) &&  (icol >= 0 && icol < cols) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Question that has a property called Type. Based on
I have a class called imageResizing. It has a member, MAX_WIDTH_ORIGINAL_IMG which is defined
I have a class called class Car , which has be instantiated as the
i have a class called (User) and i want to make a multidimensional array
I have a class called Foo that has a function that looks like the
Java Code In Java code I have class called IdentificationResult which has 3 members:
I have a class called Type: class Type { ... } Which has a
I have a class called people that has its own name, id etc... I
I have a class called 'Company' that has properties like 'CompanyName', 'CompanyCode' and 'IsActive'.
I have a class called EventConsumer which defines an event EventConsumed and a method

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.