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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:11:21+00:00 2026-05-27T07:11:21+00:00

I have a Matrix class and I want to pass an array to the

  • 0

I have a Matrix class and I want to pass an array to the constructor in order to set the values of the matrix dynamically. I found that if I allocate an array like this:

double **array;
array = new double*[3];
array[0] = new double[2];
array[1] = new double[2];
array[2] = new double[2];
array[0][0] = 1;
array[0][1] = 1;
array[1][0] = 1;
array[1][1] = 1;
array[2][0] = 1;
array[2][1] = 1;

I can get the number of rows and cols using a method like this:

int getNRows(double **data){
  int size = 0;
  while(*(data+size)){size++;}
  return size;
}

int getNCols(double **data){
  int size = 0;
  while(**(data+size)){size++;}
  return size;
}

Is this ok or should I stick to the vector declaration?

  • 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-27T07:11:22+00:00Added an answer on May 27, 2026 at 7:11 am

    Your assumption is totally wrong; you cannot obtain the size in any way close to what you propose. That’s utterly undefined and dangerous behaviour.

    Here’s a pseudo-rule (i.e. it’s not true, but unless you understand why it’s not true, it applies to you):

    Don’t use pointers. Don’t use new and delete. (And don’t say using namespace std;.)

    The one and only way in which you should be doing this is with C++ containers.

    A vector of vectors would be the first shot, though a flat vector accessed in strides may be better, and Boost.multi_array may even be the best:

    • std::vector< std::vector<double> > v (3, std::vector<double>(2));

    • std::array<std::array<double, 2>, 3>

    • std::vector<double> v(6);, and use v[i + 2 *j] etc.

    • Boost.MultiArray

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

Sidebar

Related Questions

I have a class to parse a matrix that keeps the result in an
I have a class and I want to create an array of a number
I have a matrix called matrix that looks like this: charge convexhull_xCoor convexhull_yCoor id
Hey! I have matrix class and i want to add, multiply and take transpose
I have a class Matrix which contains ArrayList<ArrayList<Double>> matrix; inside, and I want to
I have a camera class with two reference variables. I want to pass in
Basically, I have a matrix class like this (with a lot of operator overloads
I have a code base, in which for Matrix class, these two definitions are
Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix in SQL reporting and I would like it to print

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.