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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:17:51+00:00 2026-05-30T19:17:51+00:00

I am trying to pass a double* which holds an array of doubles, into

  • 0

I am trying to pass a double* which holds an array of doubles, into the constructor of my class, and assign the value element for element.

Main:

int main()
{
    double* data = new double[4];
    data[0] = 1.1; data[1] = 2.2; data[2] = 3.3; data[3] = 4.4;

    Matrix a(2,2,data);

    return 0;
}

And this is my constructor:

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

    cout << "Matrix Constructor: M: " << M << "\tN: " << N << endl;

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

}

Any attempt to index past input_data[0] in the constructor causes a crash. data[] can be accessed fine, and I can index past input_data[0] of data in my main function.

I’m assuming this should not be done this way, and would appreciate any nudge in the right direction.

  • 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-30T19:17:53+00:00Added an answer on May 30, 2026 at 7:17 pm

    It seems that you’re not allocating memory for data in your constructor.

    Matrix::Matrix(int M, int N, double* input_data)
    {
       data = new double[M*N];
       //....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to pass two parameters to a function, i being an int value
I am trying to dynamically pass a double array from C# to some JavaScript
I am trying to take the entire post array and put each value into
I am trying to pass a member function within a class to a function
I'm trying to pass in a Base64 string into a C#.Net web application via
I'm trying to pass a null value for the first parameter in the code
I'm trying to pass an instance of a class between two separate classes, like
I am trying to pass a ruby array from one C function to another
I am trying to pass my unitofwork into my generic base repository, but when
I'm currently trying to pass a mono threaded program to multithread. This software do

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.