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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:35:25+00:00 2026-05-21T20:35:25+00:00

I would like to create something like a pointer to a 2 dimensional array

  • 0

I would like to create something like a pointer to a 2 dimensional array of pointers (with the width and the height of x).
Will this code do what I expect? (Create array elements, writing out some information about them, then release all the allocated memory.)

int main(int argc, char** argv) {
    int x = 3;
    Node ***array = new Node**[x];
    for (int i = 0; i < 3; i++) {
        array[i] = new Node*[x];
        for (int j = 0; j < x; j++) {
            array[i][j] = new Node(i, j); /* Node::Node(int row, int col) */
        }
    }
    /* ....... */
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < x; j++) {
            cout << array[i][j]->row << ", " << array[i][j]->col << endl;
        }
    }
    /* ....... */
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < x; j++) {
            delete array[i][j];
            //array[i][j] = NULL;
        }
        delete[] array[i];
        //array[i] = NULL;
    }
    delete[] array;
    //array = NULL;
    return 0;
}

Or should I create a vector of vector of pointers to Node objects?
Or else should I allocate my objects on the stack?

(I’m using pointers, because in Java or C#, you have to always use the new keyword when creating an object (however, I don’t think all the objects are in the heap memory), and I read that there are more space available on the heap.)

An other reason I use pointers with the new keyword, that I would like to create multiple pointers to the same object.
Should I create one object on stack, and just create pointers to that object?

  • 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-21T20:35:26+00:00Added an answer on May 21, 2026 at 8:35 pm

    I recommend that you use vector< vector<Node> >, boost::multi_array, or you can roll-up your own dynamic 2D array class (it’s not that hard) that is a wrapper around a flat 1D std::vector.

    All of the above solutions will store your Node objects in the heap, and will take care of cleaning up memory.

    Here’s an example of a simple Matrix class that is a wrapper around std::vector<T>:

    #include <iostream>
    #include <vector>
    
    template <class T>
    class Matrix
    {
    public:
        Matrix() : width_(0), height_(0), vec_(0) {}
    
        Matrix(size_t width, size_t height)
            : width_(width), height_(height), vec_(width*height) {}
    
        size_t size() const {return vec_.size();}
    
        size_t width() const {return width_;}
    
        size_t height() const {return height_;}
    
        // Clears all preexisting data
        void resize(size_t width, size_t height)
            {width_ = 0; height_ = 0; vec_.clear(); vec_.resize(width*height);}
    
        void clear() {width_ = 0; height_ = 0; vec_.clear();}
    
        T& operator()(size_t col, size_t row) {return vec_[row*width_ + col];}
    
        const T& operator()(size_t col, size_t row) const
            {return vec_[row*width_ + col];}
    
    private:
        size_t width_;
        size_t height_;
        std::vector<T> vec_;
    };
    
    int main()
    {
        Matrix<double> a(3, 4);
        a(1, 2) = 3.1415;
        std::cout << a(1,2) << "\n";
    }
    

    It uses operator() to mimic the array[2][4] syntax of c-style multidimensional arrays. You don’t have to worry about shallow copies, freeing memory, etc, because vector already takes care of that.

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

Sidebar

Related Questions

Is there something like Python's getattr() in C#? I would like to create a
I would like create a web service in ASP.Net 2.0 that will supports JSON.
Need for two dimensional array of objects that could be something like: myContainer<myObject*> *a
I would like create my own collection that has all the attributes of python
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to create a database backed interactive AJAX webapp which has a
I would like to create an SSL connection for generic TCP communication. I think
I would like to create a file format for my app like Quake, OO,
I would like to create a stored procedure in MySQL that took a list
I would like to create an application wide keyboard shortcut for a Java Swing

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.