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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:18:24+00:00 2026-05-31T16:18:24+00:00

I have narrowed down my issue to a derived classes copy constructor, but I

  • 0

I have narrowed down my issue to a derived classes copy constructor, but I am unsure of the cause.
EDIT: M, N and Data are Private. The error I recieve is ‘Invalid allocation size: 4294967295 bytes’ – which I understand is caused when passing a -1 to new. I’m unsure why this would occur unless the data is lost when the class comunicate.

BinaryMatrix::BinaryMatrix(const BinaryMatrix& copy) : Matrix(copy)
{
    //cout << "Copy Constructor\n";

    M = copy.M;
    N = copy.N;

    data = new double[M*N]; //This line causes the allocation error

    for (int i = 0; i < M; i++)
    {
            for (int j = 0; j < N; j++)
            {
                    data[i*N+j] = copy.data[i*N+j];
            }
    }
}

The above is my derived copy constructor which causes the error. I have marked the allocation line.

I can only assume that M and N are not being read correctly. Though I am unsure why. I’ll include both derived and base constructors, and the base copy as well.

Thanks for any assistance.

MATRIX (BASE) CONSTRUCTOR

Matrix::Matrix(int M, int N, double* input_data)
{
    this->M = M;
    this->N = N;

    //cout << "Matrix Constructor\n";
    data = new double[M*N];

    for (int i = 0; i < M; i++)
    {
            for (int j = 0; j < N; j++)
            {
                    data[i*N+j] = input_data[i*N+j];
            }
    }

    delete [] input_data;
}

MATRIX (BASE) COPY CONSTRUCTOR

Matrix::Matrix(const Matrix& copy)
{
    //cout << "Copy Constructor\n";

    M = copy.M;
    N = copy.N;

    data = new double[M*N];

    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            data[i*N+j] = copy.data[i*N+j];
        }
    }
}

BINARYMATRIX (DERIVED) CONSTRUCTOR

BinaryMatrix::BinaryMatrix(int M, int N, double* input_data) : Matrix(M, N, input_data)
{
    data = new double[M*N];

    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            this->data[i*N+j] = this->getRead(i, j);
        }
    }

    double thr_val = this->Mean();

    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (this->data[i*N+j] > thr_val)
                this->data[i*N+j] = 1;

            if (this->data[i*N+j] < thr_val)
                this->data[i*N+j] = 0;
        }
    }
}
  • 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-31T16:18:25+00:00Added an answer on May 31, 2026 at 4:18 pm

    If M and N are private to Matrix and BinaryMatrix derives from Matrix, I am not sure why your code compiles (you should not be able to access M, N in BinaryMatrix). If your declaration of BinaryMatrix also includes members M and N (as well as Matrix::N and Matrix::M) then this could be the source of the problem.

    If BinaryMatrix does not declare M and N, then I think we still don’t have enough data to diagnose your problem. To guess a bit, perhaps M*N does not fit into the type used for M. So you have an arithmetic overflow. The array size is specified in size_t, so a cast will work OK.

    Also, you probably want to delegate the management of the data to exactly one of the classes. That is, do either this:

    BinaryMatrix::BinaryMatrix(const BinaryMatrix& copy) : Matrix(copy)
    {
        // M, N, data already set in Matrix::Matrix(const Matrix&)
        // The data has also been copied, so there is nothing to do here.
    }
    

    or this:

    #include <algorithm>
    
    BinaryMatrix::BinaryMatrix(const BinaryMatrix& copy) 
     : Matrix(), M(0), N(0), 
       data(0) // null in case new throws an exception (avoid bad delete in dtor).
    {
        const size_t nelems(size_t(copy.M)*size_t(copy.N));
        data = new double[nelems];
        M = copy.M;
        N = copy.N;
        stl::copy(copy.data, copy.data+nelems, data);
    }
    

    I think generally it is not a good idea to use int for iterating over dynamic data structures, since nothing guarantees that the actual size of the structure fits into int. That guarantee does exist however for size_t (any existing object must have a size representable in size_t, so you can iterate over any contiguous object using size_t).

    In fact, I’m not really sure what distinction (of purpose or behaviour) you’re trying to get from Matrix and BinaryMatrix. The data seems to have the same representation. If it is a difference of behaviour but not representation you are looking for, I think it might be better to use composition (that is, a separate un-inherited representation class) rather than inheritance. See What is the Liskov Substitution Principle? for an explanation of how to think usefully about when to use inheritance.

    However, if none of the answers you have seen so far actually helps to solve your problem, you should put some time into cutting down your example: what is the smallest complete example program which demonstrates your problem? Post that.

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

Sidebar

Related Questions

I am writing a hashing table and have narrowed down where the issue is
I have narrowed down an ugly bug, but since it seems like something internal
I spoke about this in a previous question, but I have since narrowed down
I have narrowed down my problem to the mysql_connect call I am doing in
With Java and Perl background, I have narrowed down the web framework choices to
Based on some standard web searching, I have narrowed down my problem to this:
Trying to debug an issue with saving multicharacter data to the database, I narrowed
I am debugging a defect and have narrowed it down to the vtable pointer
Alright, I have my code, and I believe I have narrowed down the crashing
I have narrowed down the culprit to either a SPAN or a DIV in

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.