0

"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] ?

quantumshiv
  • 97
  • 10

1 Answers1

0

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]; 
Raghuram
  • 3,937
  • 2
  • 19
  • 25
  • I'm sorry, I didn't quite understand. Let me back up a little bit. Lets say MatrixBuffers is a pointer to 496 doubles say P[0]----P[495]. When this is executed, MatrixBuffers[0] = new double[header.img* 12]; Is MatrixBuffers[0] a pointer to 496*12 doubles ? say K[0]---K[47520]. My understanding is that P[0] is pointing to K[0]. Is it same as writing the below code in the for loop and comment the one above it? MatrixBuffers[i] = new Double[12]; I appreciate your help!Thank you – quantumshiv Jan 22 '14 at 04:36
  • Yes...for every index we allocate or assign memory of 12 bytes. As i explained, either you can allocate once and assign the same to different index or allocate for every index individually. – Raghuram Jan 22 '14 at 04:45
  • So, in the given code: MatrixBuffers[0] has memory for 496*12 doubles. And MatrixBuffers[1 to 495] will have memory for 12 bytes? – quantumshiv Jan 22 '14 at 05:32
  • Yes you got it right...As we allocate MatrixBuffers[0] will have 496*12 doubles and we assign 12 bytes for every index from MatrixBuffers[1 to 495] – Raghuram Jan 22 '14 at 05:34
  • Don't you think its a redundant allocation of memory for MatrixBuffers[0] as we will be allocating the same 12 bytes to MatrixBuffers[1 - 495]. I just wanna know 1 good reason for this type of code. – quantumshiv Jan 22 '14 at 05:47
  • Its not redundant allocation. We allocate only once and assign the memory by incrementing the address to individual indexes. I don't know a good reason for this type of code. – Raghuram Jan 22 '14 at 05:49
  • Aah. I think I got it! Thank you so much!! – quantumshiv Jan 22 '14 at 05:54