Reputation: 13
int** a = new int*[n]();
I can't find any explanation for this code, all I find is explanations for code like int *array = new int[n];
. I understand it but I can't completely figure out the code above. What does it mean?
Upvotes: 1
Views: 3371
Reputation: 1
As you (should) know, int *a = new int[n];
allocates an array of ints with size n.
So, in general, T * a = new T[n];
allocates an array of Ts with size n.
Now if you substitute T = int *
, you'll get int **a = new int*[n];
, which allocates an array of int *s
(that is, of pointers to int).
Adding () on the right zeroes every pointer in the array (otherwise they would be uninitialized).
Upvotes: 0
Reputation: 96759
As you (should) know, int *a = new int[n];
allocates an array of int
s with size n
.
So, in general, T *a = new T[n];
allocates an array of T
s with size n
.
Now if you substitute T = int *
, you'll get int **a = new int*[n];
, which allocates an array of int *
s (that is, of pointers to int
).
Adding ()
on the right zeroes every pointer in the array (otherwise they would be uninitialized).
Upvotes: 3
Reputation: 3066
create a pointer to an array of size n, where every entry is a pointer to integer
You'll get an array of pointers
Upvotes: 1