Pointer-to-pointer dynamic two-dimensional array
Asked Answered
W

6

31

First timer on this website, so here goes..

I'm a newbie to C++ and I'm currently working through the book "Data structures using C++ 2nd ed, of D.S. Malik".

In the book Malik offers two ways of creating a dynamic two-dimensional array. In the first method, you declare a variable to be an array of pointers, where each pointer is of type integer. ex.

int *board[4];

..and then use a for-loop to create the 'columns' while using the array of pointers as 'rows'.

The second method, you use a pointer to a pointer.

int **board;
board = new int* [10]; 

etc.

My question is this: which is the better method? The ** method is easier for me to visualize, but the first method can be used in much the same way. Both ways can be used to make dynamic 2-d arrays.

Edit: Wasn't clear enough with the above post. Here's some code I tried:

int row, col;

cout << "Enter row size:";
cin >> row;
cout << "\ncol:";
cin >> col;

int *p_board[row];
for (int i=0; i < row; i++)
    p_board[i] = new int[col];

for (int i=0; i < row; i++)
{
    for (int j=0; j < col; j++)
    {
        p_board[i][j] = j;
        cout << p_board[i][j] << " ";
    }
    cout << endl;
}
cout << endl << endl;

int **p_p_board;
p_p_board = new int* [row];
for (int i=0; i < row; i++)
    p_p_board[i] = new int[col];

for (int i=0; i < row; i++)
{
    for (int j=0; j < col; j++)
    {
        p_p_board[i][j] = j;
        cout << p_p_board[i][j] << " ";
    }
    cout << endl;
}
Wellbeing answered 14/4, 2013 at 17:9 Comment(5)
I'm not sure this question changes if you make it one-dimensional. int board[4] vs int *board = new int[4]. Would you agree? It may improve your answers.Propagate
Actually, it's not dynamic allocation of a 2D array anymore if you use int *board[4], it's static allocation of an array of 4 pointers.Mendoza
@JBL: Careful throwing around the word static. You're right that static allocation takes place, but automatic storage duration is used and this can get confusing. Anyway, I believe the term "dynamic array" refers to the fact that it's not just a bloody int board[4][4] in the first place i.e. it's not necessarily rectangular.Chatav
See https://mcmap.net/q/21711/-how-do-i-declare-a-2d-array-in-c-using-new The second part of this answer is, probably, the most concise & efficient solution (imho).Balkin
which one is faster though? I guess the first (because it's static)Hullabaloo
A
57

The first method cannot be used to create dynamic 2D arrays because by doing:

int *board[4];

you essentially allocated an array of 4 pointers to int on stack. Therefore, if you now populate each of these 4 pointers with a dynamic array:

for (int i = 0; i < 4; ++i) {
  board[i] = new int[10];
}

what you end-up with is a 2D array with static number of rows (in this case 4) and dynamic number of columns (in this case 10). So it is not fully dynamic because when you allocate an array on stack you should specify a constant size, i.e. known at compile-time. Dynamic array is called dynamic because its size is not necessary to be known at compile-time, but can rather be determined by some variable in runtime.

Once again, when you do:

int *board[4];

or:

const int x = 4; // <--- `const` qualifier is absolutely needed in this case!
int *board[x];

you supply a constant known at compile-time (in this case 4 or x) so that compiler can now pre-allocate this memory for your array, and when your program is loaded into the memory it would already have this amount of memory for the board array, that's why it is called static, i.e. because the size is hard-coded and cannot be changed dynamically (in runtime).

On the other hand, when you do:

int **board;
board = new int*[10];

or:

int x = 10; // <--- Notice that it does not have to be `const` anymore!
int **board;
board = new int*[x];

the compiler does not know how much memory board array will require, and therefore it does not pre-allocate anything. But when you start your program, the size of array would be determined by the value of x variable (in runtime) and the corresponding space for board array would be allocated on so-called heap - the area of memory where all programs running on your computer can allocate unknown beforehand (at compile-time) amounts memory for personal usage.

As a result, to truly create dynamic 2D array you have to go with the second method:

int **board;
board = new int*[10]; // dynamic array (size 10) of pointers to int

for (int i = 0; i < 10; ++i) {
  board[i] = new int[10];
  // each i-th pointer is now pointing to dynamic array (size 10) of actual int values
}

We've just created an square 2D array with 10 by 10 dimensions. To traverse it and populate it with actual values, for example 1, we could use nested loops:

for (int i = 0; i < 10; ++i) {   // for each row
  for (int j = 0; j < 10; ++j) { // for each column
    board[i][j] = 1;
  }
}
Aerial answered 14/4, 2013 at 17:17 Comment(5)
The first method wont work even if you use a variable to determine the array size? //int *board[x]; With a variable used you would no longer have a static number of rows, and then end up with a dynamic 2D array, right? Sorry if this seems stupid, but I'm trying to understand what you're telling me.Wellbeing
This is what I was suspecting. You're doing on of the most popular mistakes among C++ newcomers by assuming that. I did it myself when I was novice too, so it's fine. You cannot do int *board[x]; if x is not const, i.e. again known at compile-time. Try yourself, and you'll get compile error. In addition, I've expanded my answer, please read it, and try to understand again. If you have troubles, ask again.Aerial
Actually, no, you most likely will not get a compile error. Some toolchains support non-standard extensions to make that valid. (That doesn't make it a good idea, though.)Chatav
@Lightness Races in Orbit - Yeah, this is why I was so confused over this. I'm using codeblocks with GCC, which supports it through some kind of extension. The standard doesn't allow it, so I'll stick to the pointer to pointer road.Wellbeing
@Alexander Shukaev but if we need to have two dimensional array pointer-to-pointer of a list of objects from a class that does require constructor? how could be done this ? the object must be like this: TEdit bucket3 = new TEdit(this); but how we can get a **arrray with this kind of object? bucket3 = new TEdit[5](this); bucket3 = new TEdit(this)[5]; // both statements gives error Russian are the best with Cpp delphi.Shafting
M
13

What you describe for the second method only gives you a 1D array:

int *board = new int[10];

This just allocates an array with 10 elements. Perhaps you meant something like this:

int **board = new int*[4];
for (int i = 0; i < 4; i++) {
  board[i] = new int[10];
}

In this case, we allocate 4 int*s and then make each of those point to a dynamically allocated array of 10 ints.

So now we're comparing that with int* board[4];. The major difference is that when you use an array like this, the number of "rows" must be known at compile-time. That's because arrays must have compile-time fixed sizes. You may also have a problem if you want to perhaps return this array of int*s, as the array will be destroyed at the end of its scope.

The method where both the rows and columns are dynamically allocated does require more complicated measures to avoid memory leaks. You must deallocate the memory like so:

for (int i = 0; i < 4; i++) {
  delete[] board[i];
}
delete[] board;

I must recommend using a standard container instead. You might like to use a std::array<int, std::array<int, 10> 4> or perhaps a std::vector<std::vector<int>> which you initialise to the appropriate size.

Michamichael answered 14/4, 2013 at 17:16 Comment(2)
I already know most of that. I just want to know if there's any real difference in the two methods used. I posted some code in my edit to help you understand better.Wellbeing
@user2280041: Why didn't you read sftrabbit's lovingly-produced answer? It took him some time to do. I quote: The major difference is that when you use an array like this, the number of "rows" must be known at compile-time. He went on to examine other differences, which you might find interesting.Chatav
C
1

In both cases your inner dimension may be dynamically specified (i.e. taken from a variable), but the difference is in the outer dimension.

This question is basically equivalent to the following:

Is int* x = new int[4]; "better" than int x[4]?

The answer is: "no, unless you need to choose that array dimension dynamically."

Chatav answered 14/4, 2013 at 17:49 Comment(0)
S
0

This code works well with very few requirements on external libraries and shows a basic use of int **array.

This answer shows that each array is dynamically sized, as well as how to assign a dynamically sized leaf array into the dynamically sized branch array.

This program takes arguments from STDIN in the following format:

2 2   
3 1 5 4
5 1 2 8 9 3
0 1
1 3

Code for program below...

#include <iostream>

int main()
{
    int **array_of_arrays;

    int num_arrays, num_queries;
    num_arrays = num_queries = 0;
    std::cin >> num_arrays >> num_queries;
    //std::cout << num_arrays << " " << num_queries;

    //Process the Arrays
    array_of_arrays = new int*[num_arrays];
    int size_current_array = 0;

    for (int i = 0; i < num_arrays; i++)
    {
        std::cin >> size_current_array;
        int *tmp_array = new int[size_current_array];
        for (int j = 0; j < size_current_array; j++)
        {
            int tmp = 0;
            std::cin >> tmp;
            tmp_array[j] = tmp;
        }
        array_of_arrays[i] = tmp_array;
    }


    //Process the Queries
    int x, y;
    x = y = 0;
    for (int q = 0; q < num_queries; q++)
    {
        std::cin >> x >> y;
        //std::cout << "Current x & y: " << x << ", " << y << "\n";
        std::cout << array_of_arrays[x][y] << "\n";
    }

    return 0;
}

It's a very simple implementation of int main and relies solely on std::cin and std::cout. Barebones, but good enough to show how to work with simple multidimensional arrays.

Streetcar answered 30/11, 2016 at 22:37 Comment(1)
NOTE: this does not discuss deleting the arrays, and therefore doesn't discuss how to clean up the memory pointed to by the pointers. Other answers address this already.Streetcar
S
0

this can be done this way

  1. I have used Operator Overloading
  2. Overloaded Assignment
  3. Overloaded Copy Constructor

    /*
     * Soumil Nitin SHah
     * Github: https://github.com/soumilshah1995
     */
    
    #include <iostream>
    using namespace std;
            class Matrix{
    
    public:
        /*
         * Declare the Row and Column
         *
         */
        int r_size;
        int c_size;
        int **arr;
    
    public:
        /*
         * Constructor and Destructor
         */
    
        Matrix(int r_size, int c_size):r_size{r_size},c_size{c_size}
        {
            arr = new int*[r_size];
            // This Creates a 2-D Pointers
            for (int i=0 ;i < r_size; i++)
            {
                arr[i] = new int[c_size];
            }
    
            // Initialize all the Vector to 0 initially
            for (int row=0; row<r_size; row ++)
            {
                for (int column=0; column < c_size; column ++)
                {
                    arr[row][column] = 0;
                }
            }
            std::cout << "Constructor -- creating Array Size ::" << r_size << " " << c_size << endl;
        }
    
        ~Matrix()
        {
            std::cout << "Destructpr  -- Deleting  Array Size ::" << r_size <<" " << c_size << endl;
    
        }
    
        Matrix(const Matrix &source):Matrix(source.r_size, source.c_size)
    
        {
            for (int row=0; row<source.r_size; row ++)
            {
                for (int column=0; column < source.c_size; column ++)
                {
                    arr[row][column] = source.arr[row][column];
                }
            }
    
            cout << "Copy Constructor " << endl;
        }
    
    
    public:
        /*
         * Operator Overloading
         */
    
        friend std::ostream &operator<<(std::ostream &os, Matrix & rhs)
        {
            int rowCounter = 0;
            int columnCOUNTER = 0;
            int globalCounter = 0;
    
            for (int row =0; row < rhs.r_size; row ++)
            {
                for (int column=0; column < rhs.c_size ; column++)
                {
                    globalCounter = globalCounter + 1;
                }
                rowCounter = rowCounter + 1;
            }
    
    
            os << "Total There are " << globalCounter << " Elements" << endl;
            os << "Array Elements are as follow -------" << endl;
            os << "\n";
    
            for (int row =0; row < rhs.r_size; row ++)
            {
                for (int column=0; column < rhs.c_size ; column++)
                {
                    os << rhs.arr[row][column] << " ";
                }
            os <<"\n";
            }
            return os;
        }
    
        void operator()(int row, int column , int Data)
        {
            arr[row][column] = Data;
        }
    
        int &operator()(int row, int column)
        {
            return arr[row][column];
        }
    
        Matrix &operator=(Matrix &rhs)
                {
                    cout << "Assingment Operator called " << endl;cout <<"\n";
                    if(this == &rhs)
                    {
                        return *this;
                    } else
                        {
                        delete [] arr;
    
                            arr = new int*[r_size];
                            // This Creates a 2-D Pointers
                            for (int i=0 ;i < r_size; i++)
                            {
                                arr[i] = new int[c_size];
                            }
    
                            // Initialize all the Vector to 0 initially
                            for (int row=0; row<r_size; row ++)
                            {
                                for (int column=0; column < c_size; column ++)
                                {
                                    arr[row][column] = rhs.arr[row][column];
                                }
                            }
    
                            return *this;
                        }
    
                }
    
    };
    
                int main()
    {
    
        Matrix m1(3,3);         // Initialize Matrix 3x3
    
        cout << m1;cout << "\n";
    
        m1(0,0,1);
        m1(0,1,2);
        m1(0,2,3);
    
        m1(1,0,4);
        m1(1,1,5);
        m1(1,2,6);
    
        m1(2,0,7);
        m1(2,1,8);
        m1(2,2,9);
    
        cout << m1;cout <<"\n";             // print Matrix
        cout << "Element at Position (1,2) : " << m1(1,2) << endl;
    
        Matrix m2(3,3);
        m2 = m1;
        cout << m2;cout <<"\n";
    
        print(m2);
    
        return 0;
    }
    
Severson answered 21/9, 2019 at 15:32 Comment(0)
P
0
int row, col;

cout << "Enter row size:";
cin >> row;
cout << "\ncol:";
cin >> col;

    int *p_board[row];
    for (int i=0; i < row; i++)
    p_board[i] = new int[col];

    for (int i=0; i < row; i++)
    {
    for (int j=0; j < col; j++)
    {
        p_board[i][j] = j;
        cout << p_board[i][j] << " ";
    }
    cout << endl;
    }
    cout << endl << endl;

// in this method of declaring 2d array using new ..first you have created an array of pointers locally not using new so it will be created on stack not on heap then you have created an array using new on every index of p_board, these all arrays are created on heap but due to locally created p_board will make these array of no use becoz when you will use this method in any function and will try to return the p_board pointer from that function ..at that time p_board will be vanished from stack because it was created on stack ...but 2nd method is preferable to use/

 int **p_p_board;
    p_p_board = new int* [row];
    for (int i=0; i < row; i++)
    p_p_board[i] = new int[col];

   for (int i=0; i < row; i++)
   {
   for (int j=0; j < col; j++)
   {
        p_p_board[i][j] = j;
        cout << p_p_board[i][j] << " ";
    }
    cout << endl;
    }

//in this method both array will be created on heap

Partridge answered 6/9, 2022 at 19:45 Comment(1)
Hi welcome to StackOverflow. I don't really see how your answer is an improvement to the accepted answer. Moreover, I approve the comments of the accepted answer that int *p_board[row]; with row being non const is bad practice.Schooner

© 2022 - 2025 — McMap. All rights reserved.