Domanda

"header" is an object of a struct and you can consider header.img to have a value of 496. And header struct has 3 integer elements so is the value 12 bytes. (Considering 4 bytes a int)

double** MatrixBuffers = new double* [header.img];  
    MatrixBuffers[0] = new double[header.img* 12];  
    for (unsigned int i=1; i<header.img; ++i) {
        MatrixBuffers[i] = MatrixBuffers[0] + i * 12;
    }
    globaldata.adv_MatrixBuffers = MatrixBuffers;

I understand that MatrixBuffers is a pointer to 496 doubles. But I don't understand what's happening in the second line.

MatrixBuffers[0] = new double[header.img* 12];

1.Does this mean MatrixBuffers[0] is a pointer to 496*12 doubles ? 2.What is happening in the for loop ? 3.Later in the code, MatrixBuffer[0] is being passed to a function. Does this mean I am passing a pointer that is the base address to MatrixBuffers[0] ?

È stato utile?

Soluzione

For a double pointer you have to allocate memory for first as well as second dimension.

For the second level instead of allocating memory for every dimension he allocates memory at one shot

MatrixBuffers[0] = new double[header.img* 12];

Inside the for loop they move the address and assign the same to every index.

Instead he can also do like this inside the for loop and comment the line above the for loop

MatrixBuffers[i] = new double[header.img]; 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top