gpt4 book ai didi

c - 为什么在我尝试释放矩阵时显示错误?

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:15 24 4
gpt4 key购买 nike

当我尝试编译我的代码时,我得到了这个错误,我不知道为什么:

error: incompatible type for argument 1 of ‘free’ free(A[i]);

void freeMatrix(int N, double *A)
{
for(int i = 0; i < N; i++)
free(A[i]);
free(A);
}

最佳答案

没有足够的声誉来发表评论,因此写下答案。

A[i] 是 double 类型。 free() 需要一个指针。您可能是想将函数声明为

void freeMatrix(int N, double **A){
for(int i = 0; i < N; i++)
free(A[i]);
free(A);
}

问题得到澄清:矩阵最初创建为

double *A = (double *)malloc(N * N * sizeof(double));

在这种情况下,一次调用

free(A);

就够了。通常,您应该像调用 malloc() 一样频繁地调用 free()

关于c - 为什么在我尝试释放矩阵时显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55572456/

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