col_val[index] = malloc(strlen(token) + 1);报错[Error] invalid conversion from 'void*' to 'char*' [-fpermissive]
时间: 2023-11-28 16:51:07 浏览: 34
这个错误是因为 `malloc` 函数返回的是 `void` 指针,需要将其转换为 `char` 指针类型。可以使用强制类型转换来解决这个问题,如下所示:
```
col_val[index] = (char*)malloc(strlen(token) + 1);
```
或者使用 `calloc` 函数来分配内存,该函数返回的已经是指定类型的指针,不需要进行类型转换,如下所示:
```
col_val[index] = calloc(strlen(token) + 1, sizeof(char));
```
这两种方法都可以解决这个问题。但是需要注意的是,在使用 `malloc` 或 `calloc` 分配内存时,需要在使用完毕后及时释放分配的内存,以避免内存泄漏的问题。
相关问题
用c语言重新实现下列代码功能 while(dyn->d_tag!=DT_NULL){ if(dyn->d_tag == DT_NEEDED){ ++lib->depcnt; } ++dyn; } if(lib->depcnt > 0) lib->dep=malloc(sizeof(LinkMap*)*(lib->depcnt+10)); int cur=0; dyn = lib->dyn; while(dyn->d_tag!=DT_NULL){ if(dyn->d_tag==DT_NEEDED){ lib->dep[cur++] = MapLibrary(str+dyn->d_un.d_val); } ++dyn; }
以下是用 C 语言重新实现上述代码的功能:
```
Elf64_Dyn *dyn = lib->dyn;
int depcnt = 0;
while (dyn->d_tag != DT_NULL) {
if (dyn->d_tag == DT_NEEDED) {
++depcnt;
}
++dyn;
}
if (depcnt > 0) {
lib->dep = (LinkMap **)malloc(sizeof(LinkMap *) * (depcnt + 10));
}
int cur = 0;
dyn = lib->dyn;
while (dyn->d_tag != DT_NULL) {
if (dyn->d_tag == DT_NEEDED) {
lib->dep[cur++] = MapLibrary(str + dyn->d_un.d_val);
}
++dyn;
}
```
其中,`Elf64_Dyn` 是 ELF 文件中的动态链接器数组结构体类型,`LinkMap` 是动态链接器结构体类型,`MapLibrary` 是一个函数,用于加载动态链接库。在这段代码中,我们首先遍历动态链接器数组,统计该共享库所需要的依赖库数量,然后根据数量分配一定大小的依赖库存储空间。接着,我们再次遍历动态链接器数组,将需要的依赖库加载到共享库中。
// 定义池化操作 void pool(float *input, int input_width, int input_height, int width, int height, int stride, float *output, int output_width, int output_height) { for (int i = 0; i < output_height; i++) { for (int j = 0; j < output_width; j++) { float max_val = -INFINITY; for (int k = 0; k < height; k++) { for (int l = 0; l < width; l++) { int x = j * stride + l; int y = i * stride + k; if (x >= 0 && x < input_width && y >= 0 && y < input_height) { float val = input[y * input_width + x]; if (val > max_val) { max_val = val; } } } } output[i * output_width + j] = max_val; } } }改为平均池化
可以将池化操作的求最大值改为求平均值即可,具体实现如下:
```
// 定义平均池化操作
void avg_pool(float *input, int input_width, int input_height,
int width, int height, int stride,
float *output, int output_width, int output_height) {
for (int i = 0; i < output_height; i++) {
for (int j = 0; j < output_width; j++) {
float sum_val = 0.0f;
int count = 0;
for (int k = 0; k < height; k++) {
for (int l = 0; l < width; l++) {
int x = j * stride + l;
int y = i * stride + k;
if (x >= 0 && x < input_width && y >= 0 && y < input_height) {
float val = input[y * input_width + x];
sum_val += val;
count++;
}
}
}
output[i * output_width + j] = sum_val / count;
}
}
}
```
在这个函数中,我们使用了一个 `sum_val` 变量来累加每个池化窗口内的像素值,然后再除以像素数来求平均值。注意,为了避免除以 0 的情况,我们在每个池化窗口内记录了像素数,并用变量 `count` 来累加。最后,将求得的平均值存储到输出数组中即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)