-1
我有釋放被定義爲一個結構的一個實例的元素相關聯的內存問題
當指針錯誤如下:試圖免費結構元素
typedef struct WebPage {
char *url; // url of the page
char *html; // html code of the page
size_t html_len; // length of html code
int depth; // depth of crawl
} WebPage;
我嘗試釋放它使用:
static void setDestroy(set_t *set){
pair_t *temp2;
if(set!=NULL&&set->head!=NULL){
//store the head in a temporary pair
pair_t *temp = set->head;
//cycle through LL items, freeing contents and then structure instances
while(temp!=NULL){
temp2 = temp->next;
free(temp->data->url);
free(temp->data->html);
free(temp->data);
free(temp->key);
free(temp);
temp = temp2;
}
set->head = NULL;
}
free(set);
return;
}
但是我得到這個錯誤:
gcc -Wall -pedantic -std=c11 -ggdb -c -o crawler.o crawler.c
crawler.c: In function ‘setDestroy’:
crawler.c:428:22: warning: dereferencing ‘void *’ pointer
free(temp->data->url);
^
crawler.c:428:22: error: request for member ‘url’ in something not a structure or union
crawler.c:429:22: warning: dereferencing ‘void *’ pointer
free(temp->data->html);
^
crawler.c:429:22: error: request for member ‘html’ in something not a structure or union
<builtin>: recipe for target 'crawler.o' failed
什麼我可能會被錯誤地做任何想法?我試圖將temp-> data-> url作爲一個字符串無效。
謝謝!
什麼是'pair_t'類型的聲明?它看起來像'pair_t.data'是'void *',而不是'WebPage *'。 – Barmar
哦,你說得對,pair_t.data是void *。 那麼我怎樣才能把數據轉換成WebPage類型呢? – dirtyb