- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
valgrind 告诉我,我在 XX block 中有 XX 个字节,这些字节肯定丢失了记录等等
源代码在 malloc 中,但是,我认为这是因为我没有为 malloc 释放足够的内存。不管怎样,我已经提供了我认为导致堆错误的代码。
我知道我没有释放 list_remove 中的内存,我很确定这是问题的唯一来源。它可能需要一些温度变化,但我不知道这是否是唯一的问题。
list_t *list_remove(list_t *list, list_t *node) {
list_t *oldnode = node;
node->prev->next = node->next;
node->next->prev = node->prev;
if (list != oldnode) {
free(oldnode);
return list;
} else {
list_t *value = list->next == list ? NULL : list->next;
free(oldnode);
return value;
}
}
void list_free(list_t *list) {
if (list) {
while (list_remove(list, list_last(list)) != NULL) {}
}
}
list last 只是给出列表的最后一个节点。
编辑:我很抱歉没有提供足够的信息,Kerrek SB,alk。这是代码的其余部分,如您所见,malloc 出现在 newnode 中,我可以在此处开始创建新列表。该结构非常简单,有一个值和一个上一个,下一个:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "ll.h"
struct list {
char *value;
struct list *next;
struct list *prev;
};
const char *list_node_value(list_t *node) {
return node->value;
}
list_t *list_first(list_t *list) {
return list;
}
list_t *list_last(list_t *list) {
return list->prev;
}
list_t *list_next(list_t *node) {
return node->next;
}
list_t *list_previous(list_t *node) {
return node->prev;
}
static void failed_allocation(void) {
fprintf(stderr, "Out of memory.\n");
abort();
}
static list_t *new_node(const char *value) {
list_t *node = malloc(sizeof(list_t));
if (!node) failed_allocation();
node->value = malloc(strlen(value)+1);
if (!node->value) failed_allocation();
strcpy(node->value, value);
return node;
}
list_t *list_insert_before(list_t *list, list_t *node, const char *value) {
list_t *insert_node = new_node(value);
insert_node->prev = node->prev;
insert_node->next = node;
insert_node->next->prev = insert_node;
insert_node->prev->next = insert_node;
if (list == node) {
return insert_node;
} else {
return list;
}
}
list_t *list_append(list_t *list, const char *value) {
if (list) {
(void) list_insert_before(list, list, value);
return list;
} else {
list_t *node = new_node(value);
node->prev = node->next = node;
return node;
}
}
list_t *list_prepend(list_t *list, const char *value) {
if (list) {
return list_insert_before(list, list, value);
} else {
list_t *node = new_node(value);
node->prev = node->next = node;
return node;
}
}
list_t *list_remove(list_t *list, list_t *node) {
list_t *oldnode = node;
node->prev->next = node->next;
node->next->prev = node->prev;
if (list != oldnode) {
free(oldnode);
return list;
} else {
list_t *value = list->next == list ? NULL : list->next;
free(oldnode);
return value;
}
}
void list_free(list_t *list) {
if (list) {
while (list_remove(list, list_last(list)) != NULL) {}
}
}
void list_foreach(list_t *list, void (*function)(const char*)) {
if (list) {
list_t *cur = list_first(list);
do {
function(cur->value);
cur = cur->next;
} while (cur != list_first(list));
}
}
求助!它仍然给我一个堆中的内存泄漏错误...
最佳答案
如果您担心 list_free() 我建议您在源头加强删除链 下面假设,当所有完成后,您希望 *list 为 NULL(因为整个列表刚刚被删除)。
void list_free(list_t **list)
{
if (list && *list)
{
list_t* next = (*list)->next;
while (next && (next != *list))
{
list_t *tmp = next;
next = next->next;
free(tmp);
}
free(*list);
*list = NULL;
}
}
或者类似的东西。通过传递外部列表指针的地址 调用:
list_t *list = NULL;
.. initialize and use your list...
// free the list
list_free(&list);
编辑 在 OP 发布更多代码后,有几件事很明显。
list_newnode()
不会设置 prev
和 next
的值,因此它们包含垃圾。循环列表插入必须假设被插入的新节点可以是初始列表本身。看来您正在努力远比需要的努力。请记住,循环列表可以将任何 节点作为列表头,这并不比当前列表“头”被删除 时更好。发生这种情况时,必须有一种机制可以为调用者重新建立新的列表“头”。同样的机制必须允许在删除最后一个节点时将列表头设置为 NULL。
您的代码似乎在不使用指向指针的指针的情况下公开尝试执行此操作,但它们使循环链表的任务如此变得容易得多。您的代码中需要注意的其他事项:
list_prepend()
和list_append()
函数应被视为相对于列表头的核心 插入函数。其他 API(list_insert_before()
、list_insert_after()
等)应该完全相对于您要访问的有效existing 节点在之前或之后插入,正如我上面所说,返回指向新插入节点的指针总是。您将看到两个非基于根的插入器函数不再传递列表头。以下是围绕您的大部分功能构建的代码床。实际的节点放置例程已经完成,我尽我所能对其进行了评论。主要测试夹具非常简单。如果这里有重大错误,我相信 SO-watchtower 会很快指出它们,但代码的重点不仅仅是修复你的;这是一个学习的东西:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <assert.h>
// node structure
typedef struct list_t {
char *value;
struct list_t *next;
struct list_t *prev;
} list_t;
static void failed_allocation(void) {
fprintf(stderr, "Out of memory.\n");
abort();
}
// initialize a linked list header pointer. Just sets it to NULL.
void list_init(list_t** listpp)
{
if (listpp)
*listpp = NULL;
}
// return the value-field of a valid list node.
// otherwise return NULL if node is NULL.
const char *list_node_value(list_t *node)
{
return (node ? node->value : NULL);
}
// return the next pointer (which may be a self-reference)
// of a valid list_t pointer.
list_t *list_next(list_t *node)
{
return (node ? node->next : NULL);
}
// return the previous pointer (which may be a self-reference)
// of a valid list_t pointer.
list_t *list_previous(list_t *node)
{
return (node ? node->prev : NULL);
}
// return the same pointer we were passed.
list_t *list_first(list_t *headp)
{
return headp;
}
// return the previous pointer (which may be a self-reference)
// of the given list-head pointer.
list_t *list_last(list_t *headp)
{
return list_previous(headp);
}
// insert a new item at the end of the list, which means it
// becomes the item previous to the head pointer. this handles
// the case of an initially empty list, which creates the first
// node that is self-referencing.
list_t *list_append(list_t **headpp, const char* value)
{
if (!headpp) // error. must pass the address of a list_t ptr.
return NULL;
// allocate a new node.
list_t* p = malloc(sizeof(*p));
if (p == NULL)
failed_allocation();
// setup duplicate value
p->value = (value) ? strdup(value) : NULL;
// insert the node into the list. note that this
// works even when the head pointer is an initial
// self-referencing node.
if (*headpp)
{
(*headpp)->prev->next = p;
p->prev = (*headpp)->prev;
p->next = (*headpp);
(*headpp)->prev = p;
}
else
{ // no prior list. we're it. self-reference
*headpp = p;
p->next = p->prev = p;
}
return p;
}
// insert a new value into the list, returns a pointer to the
// node allocated to hold the value. this will ALWAYS update
// the given head pointer, since the new node is being prepended
// to the list and by-definition becomes the new head.
list_t *list_prepend(list_t **headpp, const char* value)
{
list_append(headpp, value);
if (!(headpp && *headpp))
return NULL;
*headpp = (*headpp)->prev;
return *headpp;
}
// insert a new node previous to the given valid node pointer.
// returns a pointer to the inserted node, or NULL on error.
list_t *list_insert_before(list_t* node, const char* value)
{
// node *must* be a valid list_t pointer.
if (!node)
return NULL;
list_prepend(&node, value);
return node;
}
// insert a new node after the given valid node pointer.
// returns a pointer to the inserted node, or NULL on error.
list_t *list_insert_after(list_t* node, const char* value)
{
// node *must* be a valid list_t pointer.
if (!node)
return NULL;
node = node->next;
list_prepend(&node, value);
return node;
}
// delete a node referenced by the node pointer parameter.
// this *can* be the root pointer, which means the root
// must be set to the next item in the list before return.
int list_remove(list_t** headpp, list_t* node)
{
// no list, empty list, or no node all return immediately.
if (!(headpp && *headpp && node))
return 1;
// validate the node is in *this* list. it may seem odd, but
// we cannot just free it if the node may be in a *different*
// list, as it could be the other list's head-ptr.
if (*headpp != node)
{
list_t *p = (*headpp)->next;
while (p != node && p != *headpp)
p = p->next;
if (p == *headpp)
return 1;
}
// isolate the node pointer by connecting surrounding links.
node->next->prev = node->prev;
node->prev->next = node->next;
// move the head pointer if it is the same node
if (*headpp == node)
*headpp = (node != node->next) ? node->next : NULL;
// finally we can delete the node.
free(node->value);
free(node);
return 0;
}
// release the entire list. the list pointer will be reset to
// NULL when this is finished.
void list_free(list_t **headpp)
{
if (!(headpp && *headpp))
return;
while (*headpp)
list_remove(headpp, *headpp);
}
// enumerate the list starting at the given node.
void list_foreach(list_t *listp, void (*function)(const char*))
{
if (listp)
{
list_t *cur = listp;
do {
function(cur->value);
cur = cur->next;
} while (cur != listp);
}
printf("\n");
}
// printer callback
void print_str(const char* value)
{
printf("%s\n", value);
}
// main entrypoint
int main(int argc, char *argv[])
{
list_t *listp;
list_init(&listp);
// insert some new entries
list_t* hello = list_append(&listp, "Hello, Bedrock!!");
assert(NULL != hello);
assert(listp == hello);
// insert Fred prior to hello. does not change the list head.
list_t* fred = list_insert_before(hello, "Fred Flintstone");
assert(NULL != fred);
assert(listp == hello);
// Hello, Bedrock!!
// Fred Flintstone
list_foreach(listp, print_str);
// insert Wilma priot to Fred. does not change the list head.
list_t* wilma = list_insert_before(fred, "Wilma Flintstone");
assert(NULL != wilma);
assert(list_next(wilma) == fred);
assert(list_previous(wilma) == hello);
// Hello, Bedrock!!
// Wilma Flintstone
// Fred Flintstone
list_foreach(listp, print_str);
list_t* barney = list_prepend(&listp, "Barney Rubble");
list_t* dino = list_insert_after(wilma, "Dino");
assert(barney != NULL);
assert(dino != NULL);
assert(listp == barney);
assert(list_previous(barney) == fred);
assert(list_next(barney) == hello);
// Barney Rubble
// Hello, Bedrock!!
// Wilma Flintstone
// Dino
// Fred Flintstone
list_foreach(listp, print_str);
// remove everyone, one at a time.
list_remove(&listp, fred); // will not relocate the list head.
// Barney Rubble
// Hello, Bedrock!!
// Wilma Flintstone
// Dino
list_foreach(listp, print_str);
list_remove(&listp, hello); // will not relocate the list head.
// Barney Rubble
// Wilma Flintstone
// Dino
list_foreach(listp, print_str);
list_remove(&listp, barney); // will relocate the list head.
// Wilma Flintstone
// Dino
list_foreach(listp, print_str);
assert(listp == wilma);
assert(list_next(wilma) == dino);
assert(list_previous(listp) == dino);
list_remove(&listp, wilma); // will relocate the list head.
// Dino
list_foreach(listp, print_str);
list_remove(&listp, dino); // will relocate the list head;
// generate a raft entries (a million of them)/
char number[32];
int i=0;
for (;i<1000000; i++)
{
sprintf(number, "%d", i);
list_append(&listp, number);
}
// now test freeing the entire list.
list_free(&listp);
return 0;
}
如果断言和转储是为了帮助验证算法的可靠性。其结果输出应与代码中的注释相匹配,为:
Hello, Bedrock!!
Fred Flintstone
Hello, Bedrock!!
Wilma Flintstone
Fred Flintstone
Barney Rubble
Hello, Bedrock!!
Wilma Flintstone
Dino
Fred Flintstone
Barney Rubble
Hello, Bedrock!!
Wilma Flintstone
Dino
Barney Rubble
Wilma Flintstone
Dino
Wilma Flintstone
Dino
Dino
最后的想法:我已经通过 valgrind 运行它并且没有发现任何泄漏。 我很肯定它不会直接满足您的需求。** 大部分会(其中一半已经存在)。
关于c - 释放循环双向链表中的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13343780/
我在具有 2CPU 和 3.75GB 内存 (https://aws.amazon.com/ec2/instance-types/) 的 c3.large Amazon EC2 ubuntu 机器上运
我想通过用户空间中的mmap-ing并将地址发送到内核空间从用户空间写入VGA内存(视频内存,而不是缓冲区),我将使用pfn remap将这些mmap-ed地址映射到vga内存(我将通过 lspci
在 Mathematica 中,如果你想让一个函数记住它的值,它在语法上是很轻松的。例如,这是标准示例 - 斐波那契: fib[1] = 1 fib[2] = 1 fib[n_]:= fib[n] =
我读到动态内存是在运行时在堆上分配的,而静态内存是在编译时在堆栈上分配的,因为编译器知道在编译时必须分配多少内存。 考虑以下代码: int n; cin>>n; int a[n]; 如果仅在运行期间读
我是 Python 的新手,但我之前还不知道这一点。我在 for 循环中有一个基本程序,它从站点请求数据并将其保存到文本文件但是当我检查我的任务管理器时,我发现内存使用量只增加了?长时间运行时,这对我
我正在设计一组数学函数并在 CPU 和 GPU(使用 CUDA)版本中实现它们。 其中一些函数基于查找表。大多数表占用 4KB,其中一些占用更多。基于查找表的函数接受一个输入,选择查找表的一两个条目,
读入一个文件,内存被动态分配给一个字符串,文件内容将被放置在这里。这是在函数内部完成的,字符串作为 char **str 传递。 使用 gdb 我发现在行 **(str+i) = fgetc(aFil
我需要证实一个理论。我正在学习 JSP/Java。 在查看了一个现有的应用程序(我没有写)之后,我注意到一些我认为导致我们的性能问题的东西。或者至少是其中的一部分。 它是这样工作的: 1)用户打开搜索
n我想使用memoization缓存某些昂贵操作的结果,这样就不会一遍又一遍地计算它们。 两个memoise和 R.cache适合我的需要。但是,我发现缓存在调用之间并不可靠。 这是一个演示我看到的问
我目前正在分析一些 javascript shell 代码。这是该脚本中的一行: function having() { memory = memory; setTimeout("F0
我有一种情况,我想一次查询数据库,然后再将整个数据缓存在内存中。 我得到了内存中 Elasticsearch 的建议,我用谷歌搜索了它是什么,以及如何在自己的 spring boot 应用程序中实现它
我正在研究 Project Euler (http://projecteuler.net/problem=14) 的第 14 题。我正在尝试使用内存功能,以便将给定数字的序列长度保存为部分结果。我正在
所以,我一直在做 Java 内存/注意力游戏作业。我还没有达到我想要的程度,它只完成了一半,但我确实让 GUI 大部分工作了......直到我尝试向我的框架添加单选按钮。我认为问题可能是因为我将 JF
我一直在尝试使用 Flask-Cache 的 memoize 功能来仅返回 statusTS() 的缓存结果,除非在另一个请求中满足特定条件,然后删除缓存。 但它并没有被删除,并且 Jinja 模板仍
我对如何使用 & 运算符来减少内存感到非常困惑。 我可以回答下面的问题吗? clase C{ function B(&$a){ $this->a = &$a; $thi
在编写代码时,我遇到了一个有趣的问题。 我有一个 PersonPOJO,其 name 作为其 String 成员之一及其 getter 和 setter class PersonPOJO { priv
在此代码中 public class Base { int length, breadth, height; Base(int l, int b, int h) { l
Definition Structure padding is the process of aligning data members of the structure in accordance
在 JavaScript Ninja 的 secret 中,作者提出了以下方案,用于在没有闭包的情况下内存函数结果。他们通过利用函数是对象这一事实并在函数上定义一个属性来存储过去调用函数的结果来实现这
我正在尝试找出 map 消耗的 RAM 量。所以,我做了以下事情;- Map cr = crPair.collectAsMap(); // 200+ entries System.out.printl
我是一名优秀的程序员,十分优秀!