简体   繁体   English

谁能解释一下这段代码?

[英]Could anyone please explain me this piece of code?

"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. “ header”是结构的对象,您可以考虑header.img的值为496。headerstruct包含3个整数元素,因此值为12个字节。 (Considering 4 bytes a int) (考虑一个int 4个字节)

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. 我知道MatrixBuffers是指向496 double的指针。 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 ? 1.这是否意味着MatrixBuffers [0]是指向496 * 12 double的指针? 2.What is happening in the for loop ? 2. for循环中发生了什么? 3.Later in the code, MatrixBuffer[0] is being passed to a function. 3.在代码中,稍后将MatrixBuffer [0]传递给函数。 Does this mean I am passing a pointer that is the base address to MatrixBuffers[0] ? 这是否意味着我正在传递作为MatrixBuffers [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. 在for循环内,他们移动地址并将其分配给每个索引。

Instead he can also do like this inside the for loop and comment the line above the for loop 相反,他也可以在for循环内执行此操作,并注释for循环上方的行

MatrixBuffers[i] = new double[header.img]; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM