- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想在下面的函数中返回一个指向链表头部结构的指针,但由于我的 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。
我为我的语法错误道歉。
最佳答案
如果我理解你的 setName
和 getName
功能正确,那么这三行就是问题所在:
...
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/
我遇到以下问题。我想读取一个包含数百万行和数百列的大型 csv。我想向下转换列的数据类型。我的方法是读取 csv,然后使用 pd.to_numeric() 对其进行向下转换。我不知道列数及其类型。在读
目前,我从 SQL server (2008) 数据库获取数据。 cyurrent的方法是使用DataTable,然后将其传递并使用。 if (parameters != null)
我有以下问题。我有一个巨大的 csv 文件,想用多处理加载它。对于一个包含 500000 行和 130 列不同数据类型的示例文件,Pandas 需要 19 秒。我试过 dask 因为我想多处理阅读。但
是否有关于用于序列化各种 MFC 数据结构的二进制格式的明确文档?我已经能够在十六进制编辑器中查看我自己的一些类,并使用 Java 的 ByteBuffer 类读取它们(使用自动字节顺序转换等)。 但
我正在使用 Selenium 进行测试,我们用 HTML 文件编写测试用例,并用它们制作测试套件,我们的要求是编写足够健壮的测试用例,以根据测试环境改变自身。 为此,我不希望在 HTML 脚本本身中包
我需要一个 JavaScript 代码来读取存储为 .txt 文件的字典(或者也可以保存为任何其他类型的文件。它也可以在线获得)并将其内容存储在一个变量中。我不能找到一种让 JavaScript 像
我正在尝试遍历包含 SSH 登录和其他日志的日志文本文件。 程序正在返回 SSH 登录的总数。 我的解决方案确实有效,但似乎有点慢(在 200mo 文件上大约需要 3.5 秒)。我想知道是否有任何方法
我正在将大量数据从一个电子表格复制到工作簿中的其他 160 个电子表格。目前,Excel (2013) 遇到错误,因为它没有足够的资源来完成操作。 我的目标是将工作表 4 中 V13:XI1150 范
我正在尝试读取一个有 1147 行的文本文件。下面的代码仅读取第 1050-1147 行。我的目标是读取整个文件并提取位于不同行的特定值以在脚本中使用。一个示例是包含“BlockList: 2”的行中
我正在为游戏编写解释器。用户将其移动输入解释器,程序执行该移动。 现在我想为每个决定实现一个时间限制。玩家不应该能够思考超过 30 秒来写一个移动并按下回车。 call_with_time_limit
以this file例如,我正在尝试读取 data.frame 中的数据。来自 the doc (pdf 文件,表 1),它遵循一些 fortran 约定。我尝试了以下但收效甚微: dir 0' 将
我正在使用 R 阅读 Outlook 附件。我的引用在这里:Download attachment from an outlook email using R 这是我的电子邮件的截图: 这每天都会发送
我不会从表格中读取行来将主题放在列表中 php脚本 $url_obj='http://'.$host.':8069/xmlrpc/object'; $sock=new xmlrpc_client($u
我有一个这样的 csv 文件: id,name,value 1,peter,5 2,peter\,paul,3 我如何读取此文件并告诉 R "\," 不表示新列,仅表示 ","。 我必须添加该文件
我正在尝试读取 ~/Library/Preferences/com.apple.mail.plist (在 Snow Leopard 上)以获取电子邮件地址和其他信息以进入“关于”对话框。我使用以下代
This question already has answers here: How do I use floating-point division in bash? (19个回答) 5个月前关闭
本练习的目标是读取输入文件并将其存储到表中,然后验证输入中的某些字段并输出任何错误记录。我需要读取并存储每个策略组,以便表中一次仅存储 5 条记录,而不是整个文件。 所以我需要读取一个包含 5 条记录
据我了解,LWT 插入始终以 SERIAL 一致性级别完成。如果为 true,这是否意味着读取作为 LWT 插入的行可以安全地以 ANY 的一致性级别读取? 换句话说,我假设 LWT 插入是完全一致的
我看到很多很多通过java脚本读取cookie的函数,但我只想在变量中使用它一次,我是JS新手。 这是我的代码 var TheNumber = (Math.random() + '') * 10000
我正在使用 asp.net 和 C#。我在服务器上部署了一个应用程序[已发布],现在我想查看该网站的代码,据我所知,我可以阅读程序集来查看代码。 请告诉我如何实现它。 提前致谢。 最佳答案 您可以使用
我是一名优秀的程序员,十分优秀!