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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:47:40+00:00 2026-06-18T08:47:40+00:00

Solved Solution: provide the copy constructor and follow the rule of three. Original problem

  • 0

Solved

Solution: provide the copy constructor and follow the rule of three.

Original problem

I’m implementing a Class that is a Matrix of intervalls.
My Intervall Class looks like this (shortned):

class Intervall
{
friend std::ostream& operator<<(std::ostream& output, const Intervall& i);

public:
    Intervall();
    Intervall(double n);

private:
    double min_value;
    double max_value;
};

With this implementation:

Intervall::Intervall(){
    min_value = 0;
    max_value = 0;
}

Intervall::Intervall(double n){
    min_value = n;
    max_value = n;
}

ostream& operator<<(ostream& output, const Intervall& i){
    output << "[" <<  i.min_value << ", " << i.max_value <<"]";
    return output;
}

My Matrix class looks like this:

class IntervallRMatrix
{
friend std::ostream& operator<<(std::ostream& output, const IntervallRMatrix& irm);

public:
   IntervallRMatrix();
   ~IntervallRMatrix();

   void setIdentity();

   IntervallRMatrix clone();

private:
   void initialize();

   Intervall* data;
   Intervall** cells;
};

with the following implementation:

IntervallRMatrix::IntervallRMatrix(){
    initialize();
}

IntervallRMatrix::~IntervallRMatrix(){
    delete cells;
    delete data;
}

ostream& operator<<(ostream& output, const IntervallRMatrix& irm){
    output << "3 x 3" << endl;
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            output << irm.cells[i][j];
        }
        output << endl;
    }
    return output;
}

void IntervallRMatrix::setIdentity(){
    cells[0][0] = Intervall(1);
    cells[1][1] = Intervall(1);
    cells[2][2] = Intervall(1);
}

IntervallRMatrix IntervallRMatrix::clone(){
    IntervallRMatrix result;
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            result.cells[i][j] = cells[i][j];
        }
    }
    return result;
}

void IntervallRMatrix::initialize(){
    data = new Intervall[9];
    for(int i=0; i<9; i++){
        data[i] = Intervall();
    }
    cells = new Intervall*[3];
    for(int i=0; i<3; i++){
        cells[i] = &data[3*i];
    }
}

This all works fine until I use the clone function. This code produces a memory error:

int main()
{
    IntervallRMatrix m1;
    m1.setIdentity();
    cout << "m1: " << m1 << endl;

    IntervallRMatrix m2 = m1.clone();
    cout << "m2: " << m2 << endl;
    return 0;
}

The first cout works as intended.
The second cout doesn’t. The error comes right when the program tries to read m2.cells[0][0].

This error does not occur if I don’t delete cells and data in the destructor.

I declare cells and data in the way I do because I want to overload the square brackets so that I can later write Intervall = Matrix[i][j].
I also want to overload the arithmetic operators so that I can write Matrix m3 = m2*m1.
In fact, I already did that, that’s where the error first occured.

Now, finally my Question:
How can I implement a function that returns an IntervallRMatrix that won’t cause a memory error while still being able to free the allocated memory in the destructor?

  • 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-18T08:47:42+00:00Added an answer on June 18, 2026 at 8:47 am

    The issue here is that the clone method returns a copy of the result object you just created on the stack and initialised.
    As you did not implment any copy constructor (from what we can see) a default one is provided by the compiler. This default implementation will basically copy the member variable from one instance to another (in this case, the pointers to cells and data). The content will not be duplicated at all.
    Once you get out of the clone function scope, the result object gets deleted (as pre desctructor code) and you end up with a copy of result with the cells and data pointers still pointing to the just free memory. This leads to an undefined behavior (often read as “it will crash”)

    Follow the Rule of Three as Oli Charlesworth put in his answer.

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

Sidebar

Related Questions

Problem Solved - See bottom for solution notes I'm trying to build a simple
I can't find a solution for that. I managed to solve the problem of
I have a Visual Studio 2010 Solution that contains three projects: A Windows Service
SOLVED. Code has been edited to reflect solution. Given the following GridView : <asp:GridView
EDIT: solved, look below for my solution. first of all, this is my very
(Nearly solved, but not quite) (My temporary solution is to use a void method
I am trying to solve a little problem since yesterday, but no solution found
NOTICE: THIS IS SOLVED, I WILL PUBLISH THE SOLUTION HERE ASAP. Hey all, Ok...
I am looking for a solution to 8-puzzle problem using the A* Algorithm .
I've asked a similar question before here and the answer solved my problem. However,

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.