gpt4 book ai didi

在C中创建图像内核释放内存错误

转载 作者:行者123 更新时间:2023-11-30 16:25:36 28 4
gpt4 key购买 nike

我正在尝试用 C 创建一个高斯滤波器内核来进行一些图像处理。我在堆上使用 2d float 组,但是当我在行上调用 free() 时,我不断收到 free():无效指针错误。我已将过滤器的内存位置和值打印到标准输出,一切似乎都是我所期望的

//kernel->kernel = float **
//kernel->row_len = kernel->col_len = 5

float total_weight = 0.0;

//build the holding col
kernel->kernel = malloc(sizeof(float *) * kernel->col_len);
//get mem for each row and set the values
for (int j = 0; j < kernel->col_len; j++)
{
kernel->kernel[j] = malloc(sizeof(float) * kernel->row_len);
for (int i = 0; i < kernel->row_len; i++)
{
kernel->kernel[j][i] = ken_ComputeGuassianVal(i, j, sigma, size);
total_weight += kernel->kernel[j][i];
}
//print debugging info
printf("Create - %p\n", (kernel->kernel + j));
for (int i = 0; i < kernel->row_len; i++)
{
printf("%d, %d - %f \n", i, j, kernel->kernel[j][i]);
printf("%p\n", (*(kernel->kernel + j) + i));
}
printf("\n");
}

//Normalise the kernel otherwise brightness will be added to the image
for (int j = 0; j < kernel->col_len; j++)
{
for (int i = 0; i < kernel->row_len; i++)
{
kernel->kernel[j][i] /= total_weight;
}
}
for (int j = 0; j < kernel->col_len; j++)
{
printf("Attempting to free memory at location %p\n", (kernel->kernel + j));
free(kernel->kernel + j);
printf("\n");
}
free(kernel->kernel);

这是我得到的标准输出的输出

Create - 0x55aa2a80d4e0
0, 0 - 0.000000
0x55aa2a7ed8d0
1, 0 - 0.000000
0x55aa2a7ed8d4
2, 0 - 0.000001
0x55aa2a7ed8d8
3, 0 - 0.000000
0x55aa2a7ed8dc
4, 0 - 0.000000
0x55aa2a7ed8e0

Create - 0x55aa2a80d4e8
0, 1 - 0.000000
0x55aa2a84c6a0
1, 1 - 0.001083
0x55aa2a84c6a4
2, 1 - 0.034551
0x55aa2a84c6a8
3, 1 - 0.001083
0x55aa2a84c6ac
4, 1 - 0.000000
0x55aa2a84c6b0

Create - 0x55aa2a80d4f0
0, 2 - 0.000001
0x55aa2a7f96a0
1, 2 - 0.034551
0x55aa2a7f96a4
2, 2 - 1.102181
0x55aa2a7f96a8
3, 2 - 0.034551
0x55aa2a7f96ac
4, 2 - 0.000001
0x55aa2a7f96b0

Create - 0x55aa2a80d4f8
0, 3 - 0.000000
0x55aa2a80d510
1, 3 - 0.001083
0x55aa2a80d514
2, 3 - 0.034551
0x55aa2a80d518
3, 3 - 0.001083
0x55aa2a80d51c
4, 3 - 0.000000
0x55aa2a80d520

Create - 0x55aa2a80d500
0, 4 - 0.000000
0x55aa2a7eddf0
1, 4 - 0.000000
0x55aa2a7eddf4
2, 4 - 0.000001
0x55aa2a7eddf8
3, 4 - 0.000000
0x55aa2a7eddfc
4, 4 - 0.000000
0x55aa2a7ede00

Destroy - 0x55aa2a7ed8d0
Attempting to free memory at location 0x55aa2a80d4e0

Destroy - 0x55aa2a84c6a0
Attempting to free memory at location 0x55aa2a80d4e8
free(): invalid pointer
[1] 13936 abort ./dipcw

我尝试了数组表示法 kernel->kernel[j] 和 (kernel->kernel + j)。我使用的是elementary linux 5.0和gcc版本7.3.0(Ubuntu 7.3.0-27ubuntu1~18.04)

编辑:更改了标准化循环中的停止条件变量,以使用与其他循环相同的变量。末尾添加了双指针的free语句

最佳答案

规则是:每个malloc()都应该有一个对应的free()

<小时/>
struct matrix {
unsigned nrow;
unsigned ncol;
float ** ptrs;
} data;

/* allocate */

unsigned row, col;

data.ptrs = malloc (data.nrow * sizeof *data.ptrs); // <<< [A]
for (row=0; row < data.nrow; row++) { // <<< [B]
data.ptrs[row] = malloc (data.ncol * sizeof *data.ptrs[0]); // <<< [C]
}


/* there should be a corresponding free for every malloc
, but in the "inside-out" order :
*/

unsigned row, col;

for (row=0; row < data.nrow; row++) { // <<< [B]
free( data.ptrs[row] ); // <<< [C]
}

free(data.ptrs); // <<< [A]
<小时/>

注意:为了简单起见,我交换了行/列,并使用 matrix.field 而不是 pointer->field

关于在C中创建图像内核释放内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53337481/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com