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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:11:40+00:00 2026-06-19T00:11:40+00:00

I looked into two different method to allocate memory for the elements of a

  • 0

I looked into two different method to allocate memory for the elements of a matrix

Method n.1

int** matrix = new int*[rows];
for (int i = 0; i < rows; ++i)
    matrix[i] = new int[cols];

Method n.2

int** matrix = new int*[rows];
if (rows)
{
    matrix[0] = new int[rows * cols];
    for (int i = 1; i < rows; ++i)
        matrix[i] = matrix[0] + i * cols;
}

I can figure out what Method n.1 does, but I can’t figure out what exactly is supposed to do the if clause in Method n.2 (I would implement it without and it doesn’t work, with the if clause, it does…)

EDIT: Here is a code showing my problem. Why does it take so long to load (~30seconds)?

http://codepad.org/uKvI8Tk3

Codepad refuses to show the output (timeout) so if you want to run it, just compile it on your own.

Also, why cout << statements are not executed once the program starts?

  • 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-19T00:11:41+00:00Added an answer on June 19, 2026 at 12:11 am

    Method n.3: write your own Matrix class, internally using a single std::vector<int> and being clever about access by (row,col) indices.

    struct Matrix
    {
      explicit Matrix(unsigned int rows, unsigned int cols) : data_(rows*cols), cols_(cols) {}
      const int& operator()(unsigned int row, unsigned int col) const
      {
        return data_[row*cols_ + col];
      }
     private:
      std::vector<int> data_;
      unsigned int cols_;
    };
    

    Edit: iff the memory overhead of a vector is an issue in the last example, you can consider using a single dynamically allocated array of length rows*cols, and make sure to call delete [] on it in the destructor.

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

Sidebar

Related Questions

I am using this query to insert data into two different tables: $query1=INSERT INTO
I looked into the implementation of SHA2 in Python and it looks like it
I recently looked into Prism (aka the Composite Application Library for WPF) and really
I've looked into the API Demos and they have an example of the gallery
I've looked into the DesignOnly attribute, but that doesn't seem to accomplish what I
I've looked into some questions here where the best programming books are listed and
When I looked into the implementation of java.util.UUID.fromString , I found that it doesn't
So I looked into this enum type, and it kind of seems like a
Ok I have looked into this, and I'm not sure if anyone else has
First you should know I have looked into many questions and none of them

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.