4

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
}
6
  • The last clause of your first paragraph is wrong. a is declared to be a pointer to an array of 5 int; that's it. it isn't pointing to anything (yet). That's what the new [] is for. Commented Dec 21, 2016 at 15:49
  • A 2D array is a pointer to an array of pointers which each one points to another array (of ints in your case). Commented Dec 21, 2016 at 15:51
  • You forgot the semi-colon after int r=5 Commented Dec 21, 2016 at 15:51
  • 3
    @Ripi2 No it is not. A 2d array(int[][]) is an array of arrays which is very different from a int** Commented Dec 21, 2016 at 15:52
  • int b[2][5] Then int** pB = b is an error but int (*pb)[5] = b is not. That's why I said "a pointer (b) to an array of pointers (pb)". Commented Dec 21, 2016 at 16:07

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.