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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:45:37+00:00 2026-05-26T12:45:37+00:00

I don’t like questions with debug sessions and memory pointers pasted, but I am

  • 0

I don’t like questions with debug sessions and memory pointers pasted, but I am forced to ask one like that.
This is a question about invoking copy constructors. I know there are already answers about that, but I didn’t find anything that would solve my problem

I have a Matrix class:

class Matrix {
    ...

    Matrix(const Matrix& other); // copy constructor, needed due to *data

    private:
    int *data;
};

Matrix contains a pointer to a static memory array data, so when I copy a Matrix, the static array should be also copied with mempy.

At one point, I want to copy Matrix object to another Matrix

debug("COPY BEGIN ");
debug("matricex before copy: " << &itsMatrix << " < " << &matrix);
itsMatrix = matrix;
debug("COPY END ");
debug("matricex after copy: " << &itsMatrix << " < " << &matrix);

The copy constructor should be invoked to copy the data. Apparently, instead of invoking the constructor, only values of the pointers are copied; later when both memory matrices are deleted, the same pointer to data is deleted twice and I have seg fault

Here is a debug session:

1: Matrix.cpp MATRUX EMPTY 0xbf901a28 with empty data 0
2: include/SubArrayMax.hpp COPY BEGIN 
3: include/SubArrayMax.hpp matricex before copy: 0xbf901a28 < 0xbf901a3c
     --- here I should see a copy constructor ---
     --- but no debug string is printed ---
4: include/SubArrayMax.hpp COPY END 
5: include/SubArrayMax.hpp matricex after copy: 0xbf901a28 < 0xbf901a3c
6: Matrix.cpp DELETE MATRIX 0xbf901a28 with data 0x81d0550
7: Matrix.cpp DELETE MATRIX 0xbf901a3c with data 0x81d0550
 --- 0x81d0550 is deleted twice ---

and this is my copy constructor:

Matrix::Matrix(const Matrix& other) // copy construcutor
{
    ...
    data = new mval_t[dim.w * dim.h];
    memcpy(data, other.data, dim.w * dim.h * sizeof(mval_t));
    debug("MATRIX " << this << " after copying, data " << data);
}

I know that copy constructors can be reduced by compiler, I tried -fno-elide-constructors and I also had seg fault.

Any hint why this happens? or maybe there is a better way to deal with copying objects with side effects?

  • 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-26T12:45:37+00:00Added an answer on May 26, 2026 at 12:45 pm

    No, in your case the assignment operator will be called.

    Just define

    Matrix& operator=(const Matrix& other);
    
    ...
    
    Matrix& Matrix::operator=(const Matrix& other) // overloaded assignment
    {
        if (this != &other) // self-assignment is usually a problem, avoid it
        {
            ...
            // Stage 1: Do all dangerous stuff that can throw.
            int* tmp = new mval_t[dim.w * dim.h];
            memcpy(tmp, other.data, dim.w * dim.h * sizeof(mval_t)); 
            // In this case the copy is safe, but for memory allocation
            // can throw
            // If you need to allocate several things, perhaps it's better
            // to use std::nothrow and check the result, otherwise
            // throwing on the second allocation would leak the first one
    
            // Stage 2: Do a safe swap.
            //    Because there is no chance of an exception now we can mutate the object
            //    in an atomic way.
            std::swap(tmp, data)
    
            // Stage 3: Release any old resource.
            delete tmp;            
        }
        return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know if this is the right place to ask this, but I will
DON'T ASK WHY but... I have a regex that needs to be case insensitive
I don't know if this has been asked before, but what i'd like to
Don't ask me why but i can't use this method because I need to
Don't ask why but I need to add class zebra to the <li> elements
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't ask why but I have the requirement to draw a border around certain
Don't ask why, but is there any way to suppress a failed linking error?
Don't know if this is an eclipse specific problem but whenever I declare a
Don't ask me how but I managed to get accidentally the following remote branches

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.