1

Tried to understand the allocation memory in heap but confused
What is the difference them :

int** p=new int*[n];

vs

int*p=new int[n];

2)the first line of code int** p=new int*[n] this is confusing a lot what does int*[n] return and new int*[n] return.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • do not spam tags. it has nothing in common with the `C` tag. – 0___________ Aug 03 '20 at 08:14
  • In general, `new T[N]` constructs an array of size N, where each element is of type T and returns the address of the array (of type T* which is equal to that of the first element). So the first gives you an array of pointers to int , the second gives you the an array of ints. – Botje Aug 03 '20 at 08:15
  • Does this answer your question? [How do I declare a 2d array in C++ using new?](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – Support Ukraine Aug 03 '20 at 08:16
  • @churill Sorry my unserstanding is incorrect. I thought that `n` is a variable. It might be just representing a fix number. The second one get the start address of the array of int, is `decay` not a proper word? – Louis Go Aug 03 '20 at 08:28
  • It is not two dimensional array only array of pointers. – 0___________ Aug 03 '20 at 08:28
  • @churill I got it, thank you. I'll remove my comment to prevent confusion. "Dynamic" allocation is not related to VLA. – Louis Go Aug 03 '20 at 08:39

4 Answers4

3

The second line of code allocates an int array of size n, no problem with it.

The first line does not allocate a 2D array. It just allocates an array of pointers. If can be used to mimic a 2D array with an array of pointers to arrays:

int **p = new int*[n];
for (int i=0; i<n; i++) {
    p[i] = new int[m];
}

It then looks like a 2D array of size n*m, in the sense that you can use p[i][j], except that rows are not contiguous.

But remember: when coding in C++, always prefere standard containers to manual allocation.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
3

The difference is exactly as the code shows

int* p = new int[n];

allocates int, but

int** p = new int*[n];

allocates int*.

You can allocate anything (more or less). If you allocate T then what you get back is a pointer to T, i.e. T*.

But there's nothing special about pointers, they can be allocated too. So if you allocate T* (a pointer to T) then what you get back is a pointer to a pointer to T (i.e. T**).

Allocating pointers is the first step to allocating a 2D array. A 2D array is just an array of 1D arrays. The pointers are the rows in the 2D array (if you like) but the columns still need to be allocated.

int** array2d = new int*[num_rows]; // allocate the rows
for (int i = 0; i < num_rows; ++i)
     array2d[i] = new int[num_cols]; // allocate the columns for row i

After executing this code array2d is a dynamically allocated 2D array with num_rows rows and num_cols columns.

john
  • 85,011
  • 4
  • 57
  • 81
  • Then you should not use the term "2D array" in your answer as it wrong and misleading – 0___________ Aug 03 '20 at 08:31
  • @P__J__ Not now that you've pointed out the issue. Sometimes it's OK to use terms loosely. – john Aug 03 '20 at 08:32
  • No it is not. By language definition the array is a continuous chunk of memory. I can `memset(array2d, 0, sizeof(array2D));` but I cant do it with array of pointers. It is not a 2D array and IT language is precise. – 0___________ Aug 03 '20 at 08:37
  • @P__J__ Well I disagree. Context is what matters. I'm answering the question in terms that I hope the OP will understand. Obviously in a different situation I might be more precise. – john Aug 03 '20 at 08:42
2

First of all - it is not two dimensional array. It is an array of pointers.

int** p=new int*[n];

then you need to allocate space for every row by iterating through this array.

To create two dimensional array you need to:

int (*x)[n] = new int[n][n]; 
0___________
  • 60,014
  • 4
  • 34
  • 74
  • The problem with the second approach is, that `n` needs to be a compile time constant in C++. C could do it with a runtime `n`, but not C++. – cmaster - reinstate monica Aug 03 '20 at 08:30
  • @cmaster-reinstatemonica C++ does not support VLAs so it has to be constant in both. – 0___________ Aug 03 '20 at 08:31
  • Nope. The outer dimension in a `new [n]` statement may be dynamic, and should be in almost all cases. Otherwise `std::vector<>` would not be implementable. Only the `` part must be known fully at compile time. – cmaster - reinstate monica Aug 03 '20 at 08:50
  • `new [n]` is not considered a VLA because the result is a pointer to the first element with the type `*` which does not contain a dynamic array size anymore. And because `new [n]` with a dynamic `n` has been allowed since the conception of the `new` keyword. – cmaster - reinstate monica Aug 03 '20 at 08:54
0

A 2D array can be dynamically allocated in C using a single pointer. This means that a memory block of size rowcolumndataTypeSize is allocated using malloc and pointer arithmetic can be used to access the matrix elements.