- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写非常简单的函数,该函数可以在过滤(并释放)某些元素的同时更新链表的元素。我已经能够导出这个实现,尽管根据 valgrind 它包含无效的空闲以及内存泄漏。我想知道实现有什么问题。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct List {
int head;
struct List *tail;
} List;
List *cons(int h, List *tail)
{
List *list = malloc(sizeof(List));
list->head = h;
list->tail = (struct List*) tail;
return list;
}
bool is_odd(int val)
{
return val % 2 != 0;
}
int square(int val)
{
return val * val;
}
void print_list(List *l)
{
while (l) {
printf("item: %d, ", l->head);
l = (List*) l->tail;
}
printf("\n");
}
List *square_odd(List *list)
{
List *new_head = NULL;
List *prev_head = NULL;
while (list != NULL) {
List *next = (List *) list->tail;
if (is_odd(list->head)) {
if (new_head == NULL) new_head = list;
if (prev_head != NULL) prev_head->tail = (struct List*) list;
list->head = square(list->head);
prev_head = list;
} else {
if (next == NULL) {
prev_head->tail = NULL;
}
free(list);
}
list = next;
}
return new_head;
}
int main()
{
List *t = NULL;
List init = {100, NULL};
t = &init;
t = cons(1, t);
t = cons(2, t);
t = cons(3, t);
t = cons(4, t);
t = cons(5, t);
t = cons(6, t);
t = cons(7, t);
t = cons(8, t);
t = square_odd(t);
List *tmp = NULL;
print_list(t);
while(t->tail != NULL) {
tmp = t;
t = (List*) t->tail;
if (tmp != NULL) free(tmp);
}
return 0;
}
valgrind 输出为:
==17692== Memcheck, a memory error detector
==17692== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==17692== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==17692== Command: ./main
==17692==
==17692== Invalid free() / delete / delete[] / realloc()
==17692== at 0x4835948: free (in /nix/store/wrj8cjkfqzi0qlwnigx8vxwyyfl01lqq-valgrind-3.15.0/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17692== by 0x40127D: square_odd (in /tmp/linked/main)
==17692== by 0x401371: main (in /tmp/linked/main)
==17692== Address 0x1ffeffeac0 is on thread 1's stack
==17692== in frame #2, created by main (???:)
==17692==
item: 49, item: 25, item: 9, item: 1,
==17692==
==17692== HEAP SUMMARY:
==17692== in use at exit: 16 bytes in 1 blocks
==17692== total heap usage: 9 allocs, 9 frees, 1,152 bytes allocated
==17692==
==17692== LEAK SUMMARY:
==17692== definitely lost: 16 bytes in 1 blocks
==17692== indirectly lost: 0 bytes in 0 blocks
==17692== possibly lost: 0 bytes in 0 blocks
==17692== still reachable: 0 bytes in 0 blocks
==17692== suppressed: 0 bytes in 0 blocks
==17692== Rerun with --leak-check=full to see details of leaked memory
==17692==
==17692== For lists of detected and suppressed errors, rerun with: -s
==17692== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
最佳答案
what is wrong with the implementation.
您正在将 init
的地址传递给 free
。 init
是一个具有自动存储期限的变量,未使用 malloc
分配,因此您无法释放
它。
因为最初t = &init
,然后在第一次调用cons
时,您执行了new_t->tail = t
,您实际上执行了 >new_t->tail = &init
.因此,在所有 cons
调用链中的最后一个元素指向 &init
后。
t->tail-> ... ->tail->tail == &init
在循环内,将 &init
的地址传递给 free
函数。
我想说,实现实际上是可以的,第一个元素的分配是错误的。
我建议,只需删除 init
并使用 malloc
创建第一个链:
int main() {
List *t = cons(100, NULL);
t = cons(1, t);
// the rest unchanged
free(NULL)
不执行任何操作。您可以将 if (tmp != NULL) free(tmp);
替换为 free(tmp)
。
关于C lang "filter map"实现与免费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59673363/
前言 俗话说得好“工欲善其事,必先利其器”,合理的选择和使用可视化的管理工具可以降低技术入门和使用的门槛。今天大姚给大家分享一款.NET Avalonia开源、免费、跨平台、快速的Git可视化管理工
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
正在学习有关 C 语言链接列表的教程。我已编译此代码并通过 valgrind 运行它。它显示了 4 次分配和 0 次释放,这是我理解的。我需要知道如何正确调用 free() 来释放分配。 代码示例:l
正如标题所说,我需要一个搜索引擎...用于mysql 搜索。我的网站是基于 PHP 的。 我打算使用 sphinx,但我的托管公司不支持全文索引! 所以一个没有全文的搜索引擎! 它应该是相当强大的,并
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 2 年前。
我正在寻找稳定和成熟的免费/开源库来比较两个图像。 我找到了这个,但我想知道你是否使用更好的! Similar images finder - .NET Image processing in C#
我有一个通用链表实现,其中包含一个指向数据的 void* 的节点结构和一个包含对头的引用的列表结构。现在这是我的问题,链表中的一个节点可能通过其 void* 持有对另一个链表的引用。当我释放包含较小列
前言 今天大姚给大家分享一款开源(MIT License)、免费、现代化风格的WPF UI控件库:ModernWpf。 项目介绍 ModernWpf是一个开源项目,它为 WPF 提供了一组现代化
LiveCharts2 LiveCharts2是一个.NET开源(MIT License)、简单、灵活、交互式且功能强大的.NET图表、地图和仪表,现在几乎可以在任何地方运行如:Maui、Uno P
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、实用的多功能原神工具箱,旨在改善桌面端玩家的游戏体验:胡桃工具箱。 工具箱介绍 胡桃工具箱是一款.NET开源(MIT
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 3年前关闭。 Improve this qu
当我这样做时,我的 meteor 应用程序运行的免费服务器的规范是什么。 meteor deploy myapp.meteor.com 规范方面 Storage size Max bandwidth
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
如果可能,我可以使用任何网络服务免费存储少量数据(考虑 XML 或 JSON)? 我想我想创建一个小型待办事项应用程序,只是探索/学习(最好是免费的),它还可以将数据备份到云端,以便他们可以在智能手机
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 2年前关闭。 Improve this qu
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
是否有任何免费/开源替代 Codesmith 可以在功能上进行比较并生成 .NET 代码? 最佳答案 几年前我做了同样的研究,发现 MyGeneration 成为 非常好。 关于.net - 免费 C
在尝试找到可以逐步执行/允许线程的haskell monad时,我发现了免费的monad data Free f a = Return a | Roll (f (Free f a)) 及其 monad
我是一名优秀的程序员,十分优秀!