gpt4 book ai didi

c - Valgrind 泄漏不确定在哪里?

转载 作者:行者123 更新时间:2023-11-30 18:56:20 26 4
gpt4 key购买 nike

不确定 16 字节到底在哪里没有被释放。任何关于最后一个免费在哪里的想法都会很棒。我对 C 和一般编程也很陌生。

==23862== HEAP SUMMARY:
==23862== in use at exit: 16 bytes in 1 blocks
==23862== total heap usage: 25 allocs, 24 frees, 2,146 bytes allocated
==23862==
==23862== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1
==23862== at 0x4A069EE: malloc (vg_replace_malloc.c:270)
==23862== by 0x400B5D: read_from_file (sorting.c:145)
==23862== by 0x40093F: main (sorting.c:73)
==23862==
==23862== LEAK SUMMARY:
==23862== definitely lost: 16 bytes in 1 blocks
==23862== indirectly lost: 0 bytes in 0 blocks
==23862== possibly lost: 0 bytes in 0 blocks
==23862== still reachable: 0 bytes in 0 blocks
==23862== suppressed: 0 bytes in 0 blocks

但是,如果我取出第 146 行,所有内容都会被释放,并且我会从 4 个上下文中收到四个错误,表示条件跳转或移动取决于未初始化的值。

==31575== Conditional jump or move depends on uninitialised value(s)
==31575== at 0x400E31: length (sorting.c:238)
==31575== by 0x400C7D: bubble_sort (sorting.c:186)
==31575== by 0x4009B8: main (sorting.c:80)
==31575==
==31575== Conditional jump or move depends on uninitialised value(s)
==31575== at 0x400DF3: display (sorting.c:225)
==31575== by 0x4009FC: main (sorting.c:83)
==31575==
==31575== Conditional jump or move depends on uninitialised value(s)
==31575== at 0x4A063A3: free (vg_replace_malloc.c:446)
==31575== by 0x400B2F: destroy (sorting.c:138)
==31575== by 0x400A08: main (sorting.c:85)
==31575==
==31575== Conditional jump or move depends on uninitialised value(s)
==31575== at 0x400B41: destroy (sorting.c:134)
==31575== by 0x400A08: main (sorting.c:85)

我的代码是:

typedef struct Data_ {

char *name;
struct Data_ *nextData;

} Data;

int main(int argc, char **argv)
{
Data *head = NULL;
const int size = atoi(argv[2]);
head = read_from_file(argv[1], size); //line 73
head = bubble_sort(head);
destroy(head);
head = NULL;
return 0;
}

Data* read_from_file(const char *file, const int size)
{
Data *head = malloc(sizeof(Data)); //line 145
head = NULL; //line 146

FILE* in;
in = fopen(file,"r");

if(file == NULL)
{
printf("Unable to open %s\n",file);
exit(1);
}

char name[MAX_STR_LEN];
int i = 0;
for(;i<size;i++)
{
fscanf(in,"%s", name);
push(&head,name);
}
fclose(in);
return head;
}

void destroy(Data* list)
{
Data *current = list;
Data *needs_freeing;

while(current)
{
needs_freeing = current;
current = current->nextData;
free(needs_freeing->name);
free(needs_freeing);
}
}

void push(Data **head, char *name)
{
Data *new = malloc(sizeof(Data));
new->name = malloc(sizeof(char)*MAX_STR_LEN);

if(new)
{
strcpy(new->name, name);
new->nextData = *head;
*head = new;
}
}

Data* bubble_sort(Data *list)
{
Data *current;
Data *previous;
char temp[MAX_STR_LEN];

int list_length = length(list);
int i, j;
for(i=1;i<list_length;i++)
{
previous = list;
current = previous->nextData;
for(j=0;j<list_length-2;j++)
{
if(strcmp(previous->name, current->name) > 0)
{
previous = swap(previous, current);
}
previous = previous->nextData;
current = current->nextData;
}
}
return list;
}

Data* swap(Data *left, Data *right)
{
char temp[MAX_STR_LEN];

strcpy(temp, left->name);
strcpy(left->name, right->name);
strcpy(right->name, temp);
return left;
}

void display(Data *list)
{
FILE* file;
file = fopen("out.txt", "w");
if(file == NULL)
{
printf("Unable to write to file\n");
exit(1);
}
while(list->nextData != NULL)
{
fprintf(file,"%s\n",list->name);
list = list->nextData;
}
fclose(file);
}


int length(Data* list)
{
int count = 0;
Data* current = list;
while(current)
{
current = current->nextData;
++count;
}
return count;
}

最佳答案

Data *head = malloc(sizeof(Data)); //line 145
head = NULL; //line 146

这是内存泄漏。您为大小为 sizeof (Data) 的对象分配内存,但随后覆盖了指针。此外,如果您计划在 *head 中读取或写入,那么在不使用分配的对象的情况下使指针无效是没有意义的。

关于c - Valgrind 泄漏不确定在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24497589/

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