What is ** in C ++

I am currently reading C ++ source code, and I came across this:

 double **out;
 // ... lots of code here 

 // allocate memory for out
 out = new double*[num];

Not quite sure what he is doing, or what it means. Is it a pointer ... to another pointer?

There is also the following:

 double ***weight;

 // allocate memory for weight
 weight = new double**[numl];

I am very confused: P, any help is appreciated.

+3
source share
4 answers

new double*[num]is an array of double pointers, i.e. each element of the array is equal double*. You can allocate memory for each element using out[i] = new double;Similarly weight- this is an array double**. You can allocate memory for each weight element with new double*[num](if it is assumed to be an array of double*)

+8
source

double. , . , , .

out = new double*[num]; // array of pointers

, out[0] :

out[0] = new double; // one double

:

out[0] = new double[num]; // now you've got a matrix
+2

,

double*[] out;

C/++,

double** out;

, . . - , . , .

double[][] out;

, , .

double ***weight;

.

+1

Basically, both pieces of code allocate an array of pointers. For distribution, it does not matter why. Proper declaration is only necessary for type checking. Square braces should be read separately and means only an array.

Consider the following code as a quick example:

#include <stdio.h>

int main()
{
    unsigned num = 10;
    double **p1, ***p2;

    p1 = new double*[num];
    p2 = new double**[num];

    printf("%d\n", sizeof(p1));
    printf("%d\n", sizeof(p2));

    delete [] p1;
    delete [] p2;

    return 0;
}

Yes, both are just pointers. And the allocated memory is sizeof (double *) * num.

+1
source

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


All Articles