Understanding 2D array with dynamic memory allocation

290 Views Asked by At

Can someone please help me understand the last line in this snippet of code? I don't understand how that dynamically allocates the 2D array. I understand that on line 6, it's creating a pointer named 'a' and pointing it to an array of integers of size 5 as defined by 'c'.

What I don't understand is how that "new int" statement works with the r thrown into the equation. Thanks in advance.

#include <iostream>
const int c = 5; //num of columns

 int main () {
   int r = 5;
   int (*a)[c];
   a = new int[r][c]; // allocate array
}
2

There are 2 best solutions below

0
On BEST ANSWER

If you have a type T and are going to allocate an array of size N then this expression

new T[N]

returns the address of the type T * of the first element of the allocated array.

So you should write

T *a = new T[N]; 

For example if T is equivalent to the type int

typedef int T;

or

using T = int;

then the above allocation can be written

int *a = new int[N];

the size of element of the array is equal to sizeof( int ) and usually is 4 bytes.

Now let's assume that you are going to allocate an array of elements of type int[M] where M is a constant integer expression.

You can write

typedef int T[M];

or

using T = int[M];

and

T *a = new T[N];

So you allocated an array of N elements where each element has size sizeof( int[M] and the pointer a points to the first element of the array.

Because T is equivalent tp int [M] you can write

int ( *a )[M] = new int [N][M]; 

that is this statement allocates an array of N elements of the type int[M] and the pointer a gets the address of the first element of the allocated array.

Returning to your program example

int r = 5 int (*a)[c]; a = new int[r][c];

you can rewrite it the following way

typedef int T[c];

or

using T = int[c];

and

T *a = new T[r];

that is this statements allocates an array of r elements (objects) of type int[c] and a is pointer to the first element of the allocated array.

0
On

You're just making a pointer to a one-dimensional array, which requires two indexes to access elements. Nothing crazy, not quite undefined behavior, but not good either.

#include <iostream>
const int c = 5; //num of columns

int main () {
    int r = 5;

    //Creates a pointer to an array of 5 integer pointers.
    int (*a)[c];

    a = new int[r][c]; // allocate array

    std::cout << *a << std::endl;
    std::cout << a << std::endl;
    std::cout << std::endl;
    std::cout << "Displaying deferenced, unallocated array." << std::endl;
    for(int i=0; i<c;++i){
        std::cout << *a[i] << std::endl;
    }

    std::cout << "Diplaying pointers in the array." << std::endl;
    std::cout << "Note how it's not a 2d array." << std::endl;
    for(int i=0;i<c;++i){
        for(int j=0;j<r;++j){
            std::cout << a[i] << " ";
        }
        std::cout << std::endl;
    }


    std::cout << "Allocating array 1d, 1d style fails..." << std::endl;
    /*
    for(int i=0;i<c;++i){
        a[i] = 23; //Syntax error! Requires two indexes.
    }
    */

    std::cout << "Allocating 1d array 2d style... success!?" << std::endl;
    for(int i=0;i<r;++i){
        for(int j=0;j<c;++j){
            a[i][j] = 13;
        }
    }

    std::cout << "Displaying allocated array." << std::endl;
    for(int i=0;i<r;++i){
        for(int j=0;j<c;++j){
            std::cout << a[i][j] << " ";
        }
        std::cout << std::endl;
    }


    delete [] a;
} 

Outputs:

0x100202ba0
0x100202ba0

Displaying deferenced, unallocated array.
0
0
0
0
0
Diplaying pointers in the array.
Note how it's not a 2d array.
0x100202ba0 0x100202ba0 0x100202ba0 0x100202ba0 0x100202ba0 
0x100202bb4 0x100202bb4 0x100202bb4 0x100202bb4 0x100202bb4 
0x100202bc8 0x100202bc8 0x100202bc8 0x100202bc8 0x100202bc8 
0x100202bdc 0x100202bdc 0x100202bdc 0x100202bdc 0x100202bdc 
0x100202bf0 0x100202bf0 0x100202bf0 0x100202bf0 0x100202bf0 
Allocating array 1d, 1d style fails...
Allocating 1d array 2d style... success!?
Displaying allocated array.
13 13 13 13 13 
13 13 13 13 13 
13 13 13 13 13 
13 13 13 13 13 
13 13 13 13 13 
Program ended with exit code: 0