我正在编写一些C代码,并尝试使用列表 . 出于某种原因,以下代码为我提供了本书的作者,年份和ISBN,但缺少 Headers . 我怀疑它是“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;
}
这是将要打印的内容,因为您可以看到 Headers 丢失,一些信息正在被删除 . 我确实尝试过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
UPDATE: 这是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;
}
2 回答
它看起来好像你还没有为
author
和title
分配任何内存 .会更好,但会限制你的 Headers /作者字符数到100 ......
所以你应该真的使用
malloc
或calloc
分配你需要的内存 - 我建议上面的代码只是为了让事情快速而肮脏......你要求提示,所以我不会给你一个完整的解决方案:
您遇到的问题与结构, Headers 和作者中的两个字段有关 . 它们是指针,而不是固定大小的变量 . 这意味着您需要为它们分配内存空间 . 您必须为每个大小决定适当的大小,为它们分配空间,然后将文件中的文本复制到分配的内存空间中 . 目前,您只为指向char字段的指针分配空间 .