C语言 如何释放读文件后分配的空指针



我通过从文件加载数据来填充这个结构。我只是通过消毒发现,以这种方式分配的字符串不自由,但我不知道如何做到这一点,因为它开始作为一个void*项目,所以我不能释放它或给我错误。我把我的代码放在这里,对于从文件中读取的每一行,都会有一个对象的字节泄漏。

=================================================================
==12111==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 86031 byte(s) in 711 object(s) allocated from:
#0 0x7ff09a17c867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
#1 0x55dd7970854b in load_dictionary (/home/matteo/Scrivania/Algo/laboratorio-algoritmi-2021-2022-main/Esercizio 2/ex2/build/main+0x154b)
#2 0x55dd797086da in main (/home/matteo/Scrivania/Algo/laboratorio-algoritmi-2021-2022-main/Esercizio 2/ex2/build/main+0x16da)
#3 0x7ff099ec9d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

我试图直接释放变量row,但progema不起作用,因为在skipList中释放变量,我无法读取或搜索它。我必须把免费的东西放在哪里?**
我把代码的分配和回收…读取文件有没有更好更快的方法来读取文件?由于进程似乎库存,它需要超过5/10分钟来读取文件,但算法不继续。它似乎没有冻结,但它什么也不做。有了消毒程序,它给出了需要释放的内存的错误。

struct _SkipList {
Node *head;
unsigned int max_level;
int (*compare)(void*, void*);
};
struct _Node {
Node **next;
unsigned int size;
void *item;
};
static unsigned int load_dictionary(char *filename,SkipList *list )
{
unsigned int words_count = 0;
FILE *fp = fopen(filename, "r");
char *line = NULL;
size_t len = 0;
if (list == NULL)
{
list = create_skip_list();
}
char *word;
while (getline(&line, &len, fp) != -1)
{
words_count++;
word = malloc((len + 1) * sizeof(char));
strcpy(word, line);
strtok(word,"n");
insert_skip_list(list, word);
}

free(line);
fclose(fp);
return words_count;
}
SkipList* create_skip_list(){
SkipList *list = malloc(sizeof(SkipList));
list->max_level = 0;
list->compare = NULL;
list->head = create_head_node(NULL,MAX_HEIGHT);
return list;
}
Node* create_head_node(void* item, int level){
if(level <1)
return NULL;
Node *node = malloc(sizeof(Node));
if(node == NULL){
printf("error malloc nodern");
/* Returning here prevent the program from accessing non allocated
* memory. */
return NULL;
}
node->item = item;
node->size = level;
node->next = (Node**)malloc(level * sizeof(Node *));
if (!node->next) {
printf("error malloc node nextrn");
free(node);
return NULL;
}
for (int i = 0; i < level; i++)
{
node->next[i] = NULL;
}
return node;
}
void delete_node_array(Node* node){
if(node == NULL) return; 
delete_node_array(node->next[0]);
node = free_node(node);
}
SkipList* delete_skip_list(SkipList *list){
if(list == NULL) return NULL;
delete_node_array(list->head);
free(list);
list=NULL;
return list;
}
Node* free_node(Node *node){

free(node->next);
free(node);
node = NULL;
return node;

}
int insert_skip_list(SkipList* list,void* item){

if (list == NULL || item ==NULL) return -1;
Node* node = create_node(item,random_level()); //is the same of create_head_node but without the initial check of the null Item
if(node == NULL){
printf("nisert_skip_list:error malloc node");
return -1;
}

if(node->size > list->max_level){
list->max_level = node->size;
}

Node *x = list->head;
for (int k = list->max_level-1; k >= 0; k--)
{
if(x->next[k] == NULL || strcmp(item,x->next[k]->item) < 0 ){
if(k < node->size){
node->next[k] = x->next[k];
x->next[k] = node;
}
}else{
x = x->next[k];
k++;
}
}
return 0;
}

我解决了:

Node* free_node(Node *node){
free(node->item); --> new line
free(node->next);
free(node);
node = NULL;
return node;

}

但这很奇怪,因为第一次没有成功。否则仍然太慢,无法得到正确的解决方案。任何建议如何提高读取功能的速度?

最新更新