当我尝试释放我创建的元组数组时出现此错误。
这里是错误发生的地方:
void free_freq_words(Tuple *freq_words, int num)
{
int i;
for (i = 0; i < num; i++)
{
free(freq_words[i].word);
free(freq_words[i]); /******* error here *********/
}
}
我创建了这样的元组数组:
Tuple *freq_words = (Tuple *) malloc(sizeof(Tuple) * num);
元组的定义如下:
typedef struct Tuple
{
int freq;
char *word;
} Tuple;
请注意,我很确定在释放 Tuple 本身之前我必须释放单词,因为我为每个单词分配了空间:
freq_words[num - 1].word = (char *) malloc(sizeof(char) * strlen(word) + 1);
我得到的错误是第二个免费的:
fw.c: In function âfree_freq_wordsâ:
fw.c:164:7: error: incompatible type for argument 1 of âfreeâ
free(freq_words[i]);
^
In file included from fw.c:3:0:
/usr/include/stdlib.h:482:13: note: expected âvoid *â but argument is of type âT
upleâ
extern void free (void *__ptr) __THROW;
我在释放前尝试施法,但没有用:
fw.c: In function âfree_freq_wordsâ:
fw.c:164:7: error: cannot convert to a pointer type
free((void *) freq_words[i]);
我以前从未遇到过关于 free 的错误,除非我尝试两次释放同样的东西,所以我不知道该怎么做。我用谷歌搜索,但找不到太多。我应该如何更改我的代码才能免费使用?
最佳答案
分配是: 元组 *freq_words = (元组 *) malloc(sizeof(元组) * num);
取消分配是: 免费(freq_words);
关于c - free 的参数 1 的类型不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50047899/