Understanding a 2D array with dynamic memory allocation

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

What I don't understand is how this "new int" operator works with 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
}
+4
source share
2 answers

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

new T[N]

T * .

,

T *a = new T[N]; 

, T int

typedef int T;

using T = int;

int *a = new int[N];

sizeof( int ) 4 .

, int[M], M .

typedef int T[M];

using T = int[M];

T *a = new T[N];

, N, sizeof( int[M], a .

T tp int [M],

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

N int[M], a .

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

typedef int T[c];

using T = int[c];

T *a = new T[r];

r () int[c] a .

+2

, . , undefined , .

#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 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;
} 

:

0x100202ba0
0x100202ba0

Displaying deferenced, unallocated array.
0
0
0
0
0
Diplaying pointers in the array.
Note how it 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
0

Source: https://habr.com/ru/post/1664462/


All Articles