[英]How to free a void pointer allocated after read a file
i have this structure filled by loading data from a file.我通过从文件加载数据来填充这个结构。 I just found out by sanitaizer that string allocated in this way doesn't be free but i don't know how to do it cause it start as a void* item so i can't free it or give me error.
我刚刚通过 sanitaizer 发现以这种方式分配的字符串不是免费的,但我不知道该怎么做,因为它以 void* 项开始,所以我无法释放它或给我错误。 I put here my code where, for every row read from the file there will be a leak of byte of an object.
我把我的代码放在这里,对于从文件中读取的每一行,都会有 object 的字节泄漏。
=================================================================
==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
I tried to free directly the variable row
but then progema doesn't work cause a free the variable in the skipList and i can't read or search in it.我试图直接释放变量
row
,但随后 progema 不起作用,导致释放了 skipList 中的变量,我无法在其中读取或搜索。 **Where i must put the free of the item? **我必须把免费的东西放在哪里? **
**
I put the code of the allocation and deallocation...with the read of file There is a better and faster way to read the file?我把分配和解除分配的代码...和文件的读取放在一起有更好更快的方法来读取文件吗? Cause the process seems to stock, it takes more than 5/10 minutes to read the file but the algorithm does not proceed.
导致进程似乎存货,读取文件需要超过 5/10 分钟,但算法不会继续。 It doesn't seem to freeze but it does nothing.
它似乎没有冻结,但它什么也不做。 With the sanitaizer it gives the error of memory that needs to be freed.
使用消毒剂,它会给出需要释放的错误 memory。
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 node\r\n");
/* 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 next\r\n");
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;
}
I solved by put in:我通过输入解决了:
Node* free_node(Node *node){
free(node->item); --> new line
free(node->next);
free(node);
node = NULL;
return node;
}
But this is strange cause the first time didn't work.但这很奇怪,因为第一次没有用。 Otherwise still too slow for a correct solution.
否则对于正确的解决方案来说仍然太慢了。 Any suggest how to improve the read function's speed?
任何建议如何提高读取功能的速度?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.