gpt4 book ai didi

c - fread 无法从 C 中的二进制文件中读取结构的字符串值

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

我想在下面的函数中返回一个指向链表头部结构的指针,但由于我的 fread 无法读取结构的字符串,我必须 malloc 一些变量并将 newheader->name string 指向它。在下面的函数中,如果我释放我的临时变量然后返回新的 header ,则只打印空值,如果我不这样做,那么我会出现内存泄漏,但函数工作正常。我希望能够返回一个完整链表的头部而不是空值。

struct head *read_strings(char *file) {
FILE *my_file;
struct head * newheader = NULL;
char *str = NULL;
newheader = buildhead();

int len, lenStr, lenTotal = 0;
printf("\n\n");
my_file = fopen(filename, "rb");
if (my_file == NULL) {
printf("error\n");
}
fread(&len,sizeof(int),1, my_file);
newheader->name = malloc(len);
fread(newheader->name,sizeof(char),len, my_file);
fread(&newheader->length,sizeof(int),1, my_file);

if (newheader->length != 0) {
if (newheader->length == lenTotal) {
fread(&lenStr,sizeof(int),1, my_file);
str = malloc(lenStr); //char *str = malloc(lenStr);
fread(str,sizeof(char),lenStr, my_file);
create_string(newheader, str);
lenTotal += lenStr;
str = NULL; //free(str);
}
else {
while (newheader->length != lenTotal) {
fread(&lenStr,sizeof(int),1, my_file);
str = malloc(lenStr); //char *str = malloc(lenStr);
fread(str,sizeof(char),lenStr, my_file);
create_string(newheader, str);
lenTotal += lenStr;
str = NULL; //free(str);
}
}
}

printString(newheader);
fclose(my_file);
free(str);
//if i free newheader->name here then I get no memory leaks
//free(newheader->name);
freeStructure(newheader);
return newheader;
}

我需要从二进制文件中读取字符串并将它们存储到结构中。但是我不能将值直接存储到结构的字符串变量中,除非我 malloc 一个新字符串并指向它。您可以在上面的代码中看到,我能够从 fread 中读取 newheader->length 而不是 newheader->name。我试图将 fread 结果放入字符串数组中,但出现段错误。

这是我的二进制文件的样子。它在每个字符串和 int 之后都有空终止符。

0000000 021  \0  \0  \0   i   t       i   s       a       g   o   o   d
0000020 d a y \0 R \0 \0 \0 # \0 \0 \0 I t
0000040 w a s t h e b e s t o f
0000060 o y e h o y e t i m e s . \0
0000100 034 \0 \0 \0 I t w a s t h e b
0000120 l u r s t o f t i m e s . \0
0000140 \a \0 \0 \0 m o n k e y \0 006 \0 \0 \0 p
0000160 a n d a \0 006 \0 \0 \0 s h i f u \0
0000177

如果我在我的 freestruct() 函数中释放 newheader->name 然后我得到以下错误

*** Error in `./test': munmap_chunk(): invalid pointer:
0x000000000040133e *** Aborted.

如果不释放它,那么我会在 1 个 block 中丢失 17 个字节。

==24169== HEAP SUMMARY:
==24169== in use at exit: 0 bytes in 0 blocks
==24169== total heap usage: 5 allocs, 6 frees, 1,201 bytes allocated
==24169==
==24169== All heap blocks were freed -- no leaks are possible
==24169==
==24169== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from0)
==24169==
==24169== 1 errors in context 1 of 1:
==24169== Invalid free() / delete / delete[] / realloc()
==24169== at 0x4C29E90: free (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==24169== by 0x401298: freestruct (A1.c:267)
==24169== by 0x400F8A: write_strings (A1.c:189)
==24169== by 0x400840: main (test.c:25)
==24169== Address 0x40133e is not stack'd, malloc'd or (recently)
free'd
==24169==
==24169== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from0)

这是我的 freestruct() 函数中的一小段代码

if (header->next == NULL) {
header->length = 0;
free(header->name);
free(header);
}

将字符串写入二进制文件后,我释放了整个结构,然后在 read_strings 函数中再次创建

编辑:如果我在 readString 函数中释放 newheader->name,那么我不会发生内存泄漏,但我想将 newheader 返回到 main 并在之后调用 printString 和 freestruct。

我为我的语法错误道歉。

最佳答案

如果我理解你的 setNamegetName 功能正确,那么这三行就是问题所在:

...
setName(newheader, name);
newheader->name = getName(newheader);
...
free(name);

首先,前两行做同样的事情,但这不是问题,只是多余的。这是导致问题的最后一行。

您使 newheader->name 指向与 name 指向的同一内存。赋值后,两个指针都指向同一个内存。然后你释放内存。这使得两个指针都无效。

您要么需要复制(例如使用非标准但常见的 strdup 函数)。或者不释放内存。


让我们看看我是否可以更清楚...

您有一个变量name,您为其分配了内存。它看起来像这样:

+------+     +---------------------+| name | --> | memory for the name |+------+     +---------------------+

What the assignment

newheader->name = name;

实际上只是复制指针,而不是它指向的内存。所以在分配之后你有这样的东西:

+------+| name | ------------\+------+              \    +---------------------+                       >-> | memory for the name |+-----------------+   /    +---------------------+| newheader->name | -/+-----------------+

You now have two variables pointing to the same memory.

Then you do

free(name);

这就是内存,但请记住我们有两个 变量指向同一个内存。现在看来

+------+| name | -------------> ?+------+           +-----------------+| newheader->name | --> ?+-----------------+

因此,每当您尝试访问 newheader->name 时,您将尝试访问现在释放的内存,并且您将有未定义的行为

关于c - fread 无法从 C 中的二进制文件中读取结构的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39683579/

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