maps[layer] = (struct block **)malloc(sizeof(struct block*)*(rows*cols+1));
时间: 2023-06-09 07:02:39 浏览: 47
我明白了您的问题,这似乎是一段代码,但缺少了一个运算符。这段代码应该是这样的:maps[layer] = (struct block **)malloc(sizeof(struct block*)*(rows*cols + 1));
它实际上是用于动态分配一个二维数组。maps是一个指向指针数组的指针,layer是一个整数用于指定数组的层数。malloc函数用于动态分配内存空间,其中sizeof(struct block*)*(rows*cols + 1)用于计算需要分配的内存大小。最终,maps[layer]将指向一个由struct block指针构成的数组。
相关问题
struct Student**c语言中
`struct Student**`是一个指向指针的结构体类型,表示一个指向结构体类型为`Student`的指针的指针。通常用于二级指针的场景,例如动态分配一个二维数组。
以下是一个示例:
```c
struct Student {
char name[20];
int age;
};
// 动态分配一个二维数组,每个元素是一个指向结构体类型为Student的指针
struct Student **students = (struct Student **)malloc(sizeof(struct Student*) * num_rows);
for (int i = 0; i < num_rows; i++) {
students[i] = (struct Student *)malloc(sizeof(struct Student) * num_cols);
}
```
在上面的示例中,我们动态分配了一个二维数组,其中每个元素都是一个指向结构体类型为`Student`的指针。可以通过`students[i][j]`访问二维数组中的每个元素。
#include <windows.h> #include <iostream> #include <time.h> using namespace std; struct Cell { char data[128]; }; const int TOTAL_ROWS = 2500; const int TOTAL_COLS = 2500; int main() { clock_t t; t = clock(); Cell* p = (Cell*)malloc(TOTAL_ROWS * TOTAL_COLS * sizeof(Cell)); if (p) { cout << "ok" << endl; } else { cout << "oops" << endl; } (p + (2 * TOTAL_COLS) + 3)->data[0] = 'A'; t = clock() - t; cout << t << endl; t = clock(); p = (Cell*)VirtualAlloc(NULL, TOTAL_ROWS * TOTAL_COLS * sizeof(Cell), MEM_RESERVE, PAGE_READWRITE); if (p) { cout << "ok" << endl; } else { cout << "oops" << endl; } VirtualAlloc(p + (2 * TOTAL_COLS) + 3, sizeof(Cell), MEM_COMMIT, PAGE_READWRITE); (p + (2 * TOTAL_COLS) + 3)->data[0] = 'A'; t = clock() - t; cout << t << endl; return 0; } 在virtual的基础上进行修改,给对角线(左上到右下那条)上所有Cells中的128个元素都赋值成'A',并用clock函数测量时间。
下面是对代码的修改和解释:
```cpp
#include <windows.h>
#include <iostream>
#include <time.h>
using namespace std;
struct Cell {
char data[128];
};
const int TOTAL_ROWS = 2500;
const int TOTAL_COLS = 2500;
int main() {
clock_t t;
t = clock();
// 使用 malloc 分配内存
Cell* p = (Cell*)malloc(TOTAL_ROWS * TOTAL_COLS * sizeof(Cell));
if (p) {
cout << "ok" << endl;
} else {
cout << "oops" << endl;
}
// 给对角线上的元素赋值为 'A'
for (int i = 0; i < TOTAL_ROWS && i < TOTAL_COLS; i++) {
(p + i * TOTAL_COLS + i)->data[0] = 'A';
}
t = clock() - t;
cout << "malloc: " << t << endl;
t = clock();
// 使用 VirtualAlloc 分配内存
p = (Cell*)VirtualAlloc(NULL, TOTAL_ROWS * TOTAL_COLS * sizeof(Cell),
MEM_RESERVE, PAGE_READWRITE);
if (p) {
cout << "ok" << endl;
} else {
cout << "oops" << endl;
}
// 分配物理内存
VirtualAlloc(p, TOTAL_ROWS * TOTAL_COLS * sizeof(Cell), MEM_COMMIT, PAGE_READWRITE);
// 给对角线上的元素赋值为 'A'
for (int i = 0; i < TOTAL_ROWS && i < TOTAL_COLS; i++) {
(p + i * TOTAL_COLS + i)->data[0] = 'A';
}
t = clock() - t;
cout << "VirtualAlloc: " << t << endl;
return 0;
}
```
上面的修改中,我们在两个内存分配方式中分别给对角线上的元素赋值为 'A'。同时,我们也修改了输出的内容,分别输出使用 malloc 和 VirtualAlloc 分配内存所需的时间。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)