gpt4 book ai didi

c - C链表中的内存泄漏

转载 作者:太空宇宙 更新时间:2023-11-04 02:33:00 25 4
gpt4 key购买 nike

我正在编写一些 C 代码,并尝试使用列表。出于某种原因,以下代码为我提供了本书的作者、年份和 ISBN,但缺少标题。我怀疑这是“insert_at_begin”函数某处的内存泄漏。虽然我真的不确定在那里做什么。该程序从包含书名、作者等的文件中读取,并应将其作为动态列表返回。

任何提示或某种帮助将不胜感激。我确实环顾了互联网和堆栈溢出,但找不到解决此内存泄漏的方法。我添加了相关代码,虽然很长,但我不确定泄漏在哪里,所以我认为这样会更好。

typedef struct _element element;


typedef struct _list {
element *first;
int count;
} list;


struct _element {
char* title;
char* author;
int year;
long long int isbn;
element *next;
};


element *insert_at_begin(element *first, element *new_elem) {

if(first) {
new_elem->next = first;
first = new_elem;

} else {
first = new_elem;
}

return first;

}


element *construct_element(char *title, char* author, int year, long long int isbn) {

element* book = malloc(sizeof(element));
book->title = title;
book->author = author;
book->year = year;
book->isbn = isbn;
book->next = NULL;
return book;
}


void free_list(list *alist) {

//Only when there is 2 elements in the list start this loops
for (int i=1; i<alist->count; i++) {
free(alist->first->next);
}

free(alist->first);
free(alist);

}


void read_list(char* filename, list *alist) {
element* new_elem;

char* title;
char* author;
int year;
long long int isbn;
read_line_context ctx;
open_file(&ctx, filename);
while(read_line(&ctx, &title, &author, &year, &isbn) == 0) {
new_elem = construct_element(title, author, year, isbn);
alist->first = insert_at_begin(alist->first, new_elem);
alist->count++;
}
}

list* construct_list() {
list *alist = malloc(sizeof(list));
alist->first = NULL;
alist->count = 0;
return alist;
}


void print_list(list *alist) {
printf("My Books\n================\n\n");
int counter = 1;
element *elem = alist->first;
while (elem != NULL) {
printf("Book %d\n", counter);
printf("\tTitel: %s\n", elem->title);
printf("\tAuthor: %s\n", elem->author);
printf("\tYear: %d\n", elem->year);
printf("\tISBN: %lld\n", elem->isbn);
elem = elem->next;
counter++;
}
}

int main(int argc, char** argv) {
list *alist = construct_list();
read_list(argc>1?argv[1]:"buecherliste.txt", alist);
print_list(alist);
free_list(alist);
return 0;
}

这就是将要打印的内容,您可以看到标题丢失了,一些信息也被截断了。我确实尝试过 X Code Leaks 工具,但在那里找不到任何有用的东西。我在 read_list 的 while 循环中添加了一个用于调试的 printf 函数,它在那里工作得很好。所以它必须与 insert_at_begin 函数有关。

My Books
================

Book 1
Titel:
Author: Phillip K. Dick
Year: 1973
ISBN: 9780547572178
Book 2
Titel:
Author: nner Darkly
Year: 1949
ISBN: 9783548267456

更新:这是 read_line 函数:

int read_line(read_line_context *ctx, char **name, char **author, int *year, long long int *isbn) {
if (ctx->filepointer == NULL){
perror(ctx->filename);
exit(1);
}
char *name_s;
char *author_s;
char *year_s;
char *isbn_s;
char *delim = ";";
ssize_t len;

if ((len = getline(&(ctx->line), &(ctx->linecapp), ctx->filepointer)) != -1)
{
/* remove tailing newline */
char *pos;
if ((pos = strchr(ctx->line, '\n')) != NULL)
*pos = '\0';

/* read individual fields */
name_s = strtok(ctx->line, delim);
author_s = strtok(NULL, delim);
year_s = strtok(NULL, delim);
isbn_s = strtok(NULL, delim);
if(name_s != NULL && author_s != NULL && year_s != NULL && isbn_s != NULL ) {
*name = name_s;
*author = author_s;
*year = atoi(year_s);
*isbn = atoll(isbn_s);
return 0;
}

}
fclose(ctx->filepointer);
ctx->filepointer = NULL;
ctx->linecapp = 0;
if (ctx->line != NULL ) {
free(ctx->line);
}
return -1;
}

最佳答案

在我看来,您似乎还没有为 authortitle 分配任何内存。

struct _element {
char title[100];
char author[100];
int year;
long long int isbn;
element *next;
};

会更好,但会将您的标题/作者字符数限制为 100...

所以你真的应该使用 malloccalloc 来分配你需要的内存 - 我建议上面的代码只是为了让事情在快速和肮脏的基础上工作。 ..

关于c - C链表中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40947034/

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