-2

I'm new in c++. I don't clearly understand the line: int** outer = new int*[n]; .

I took from solution here: https://www.hackerrank.com/challenges/variable-sized-arrays/editorial

And the problem: https://www.hackerrank.com/challenges/variable-sized-arrays/problem

Many thanks!

underscore_d
  • 6,309
  • 3
  • 38
  • 64
Minh Huy
  • 41
  • 6

2 Answers2

0

I'm new in c++. I don't clearly understand the line: int** outer = new int*[n];

It means you allocate memory to store an array of n elements of type "pointer to an integer".

As operator new returns a pointer to the first element, outer variable is of type int ** which means here "a pointer to a pointer to an integer"

This answer might also help you to understand :

How do I declare a 2d array in C++ using new?

Fryz
  • 2,119
  • 2
  • 25
  • 45
  • ty, it does help. But can you speak more deeply about the heap, or what that line really do with the memory? I'm trying to understand cleary about this, i'm really confusing about the pointer. – Minh Huy Apr 05 '18 at 15:14
  • Forget the heap for the moment. It is not necessary to understand pointers, and the compiler may not even use it. When you read about dynamic allocation in the heap, just mentally replace "heap" with "dynamic memory space", which is probably the term they should be using. – giusti Apr 05 '18 at 15:17
0

I don't clearly understand the line: int** outer = new int*[n];

In such cases it could be useful to use type alias:

using intp = int *;
intp *outer = new intp[n];

so you have a dynamically allocated array of type intp, thing that intp is a pointer as well should not confuse you and having type alias can help to understand that.

Slava
  • 43,454
  • 1
  • 47
  • 90