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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:35:39+00:00 2026-06-15T01:35:39+00:00

I have a matrix class which dynamically reallocates size. To be specific: dynamically reallocates

  • 0

I have a matrix class which dynamically reallocates size. To be specific: dynamically reallocates an array of rows.

But in some cases there is a memory freeing problem when matrix’s destructor is called:

*** glibc detected *** ./solver: free(): invalid next size (fast): 0x0000000000c112b0 ***

and the process is being aborted.

The Matrix class:

#ifndef __matrix_hpp
#define __matrix_hpp

#include <cstring>
#include <stdexcept>
#include "row.hpp"

using namespace std;

class Matrix {

public:
    Matrix();
    ~Matrix();
    size_t size();
    void resize(size_t size);
    double get(size_t x, size_t y) throw (out_of_range);
    void set(size_t x, size_t y, double value) throw (out_of_range);
    double getfv(size_t y) throw (out_of_range);
    void setfv(size_t y, double value) throw (out_of_range);
    void optimize(size_t y) throw (out_of_range);
    void _print();

private:
    size_t sz;
    Row **data;

};

#endif

And the body of significant functions:

Matrix::~Matrix() {
    if (data != NULL) {
        for (size_t i = 0; i < sz; ++i)
            delete data[i];
        delete [] data;
    }
}

void Matrix::resize(size_t size) {
    if (size == sz)
        return;
    Row **newData = new Row *[size];
    if (data != NULL)
        memcpy(newData, data, sz * sizeof(Row*));
    if (size > sz) {
        for (size_t i = sz; i < size; ++i)
            newData[i] = new Row();
    }
    else {
        for (size_t i = size; i < sz; ++i)
            delete data[i];
    }
    delete [] data;
    data = newData;
    sz = size;
}

So, that’s the code. The problem comes when I make a matrix and later reduce it’s size and call desctructor. For example:

Matrix *matrix = new Matrix();
matrix->resize(10);
matrix->resize(7);
delete matrix;

But making matrix bigger works pretty good:

Matrix *matrix = new Matrix();
matrix->resize(10);
matrix->resize(13);
delete matrix;

And the funniest thing, this example works:

Matrix *matrix = new Matrix();
matrix->resize(3);
matrix->resize(2);
delete matrix;

So I have no idea what can be wrong. Any suggestions?

  • 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-15T01:35:40+00:00Added an answer on June 15, 2026 at 1:35 am

    The error is that you write outside of your allocated space:
    The culprit is this line

    Row **newData = new Row *[size];
    if (data != NULL)
        memcpy(newData, data, sz * sizeof(Row*));
    

    If sz is bigger than size, then you will write too much and potentially destroyed your heap. Change it to the code below and everything should work better. This way you will always copy valid data and no more than you have allocated.

    Row **newData = new Row *[size];
    if (data != NULL)
        memcpy(newData, data, (size>sz?sz:size) * sizeof(Row*));
    

    That resizing from 3 to 2 works is down to luck (and how the heap works).

    Also, you don’t check if new Row[] fails but that would lead to a NULL pointer exception.

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

Sidebar

Related Questions

I have a code base, in which for Matrix class, these two definitions are
I have a class which contains a few boost::numeric::ublas::matrix's within it. I would like
I have a simple matrix class which has a 2d integer pointer field in
I have a class Matrix which contains ArrayList<ArrayList<Double>> matrix; inside, and I want to
I have a SquareMatrix template class which inherits Matrix template class, like below: SquareMatrix.h:
I have a mathematical matrix class. It contains a member function which is used
So I have a generic Matrix class that I created which has the following
In OpenCV, there is a CvSVM class which takes in a matrix of samples
I have a matrix which is built at the start of the class and
I have a template class which implements function: template<typename T> class Matrix { ...

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.