C 自由列表实现无法正常工作
因此,我被分配了一个任务来实现一个自由列表。一个列表,其中将要免费的项目:d添加到该列表中,之后该列表将一次性免费:d。我写了以下内容:C 自由列表实现无法正常工作,c,malloc,free,C,Malloc,Free,因此,我被分配了一个任务来实现一个自由列表。一个列表,其中将要免费的项目:d添加到该列表中,之后该列表将一次性免费:d。我写了以下内容: #include <assert.h> #include <stdio.h> #include <stdlib.h> typedef struct list_t list_t; struct list_t { list_t* succ; list_t* pred; void*
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct list_t list_t;
struct list_t {
list_t* succ;
list_t* pred;
void* data;
};
list_t* free_list;
void free_list_memory(void)
{
list_t *p , *q;
p = free_list;
while (p != NULL) {
q = p->succ;
free(p);
p = q;
}
}
void add_to_free_list(list_t* add) {
if (free_list != NULL) {
add->pred = free_list->pred;
add->succ = free_list;
free_list->pred->succ = add;
free_list->pred = add;
} else {
free_list = add;
add->succ = add;
add->pred = add;
}
}
static double sec(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + 1e-6 * tv.tv_usec;
}
int empty(list_t* list)
{
return list == list->succ;
}
list_t *new_list(void* data)
{
list_t* list;
list = malloc(sizeof(list_t));
assert(list != NULL);
list->succ = list->pred = list;
list->data = data;
return list;
}
void add(list_t* list, void* data)
{
list_t* link;
list_t* temp;
link = new_list(data);
list->pred->succ= link;
link->succ = list;
temp = list->pred;
list->pred = link;
link->pred = temp;
}
void take_out(list_t* list)
{
list->pred->succ = list->succ;
list->succ->pred = list->pred;
list->succ = list->pred = list;
}
void* take_out_first(list_t* list)
{
list_t* succ;
void* data;
if (list->succ->data == NULL)
return NULL;
data = list->succ->data;
succ = list->succ;
take_out(succ);
free(succ);
return data;
}
static size_t nextsize()
{
#if 1
return rand() % 4096;
#else
size_t size;
static int i;
static size_t v[] = { 24, 520, 32, 32, 72, 8000, 16, 24, 212 };
size = v[i];
i = (i + 1) % (sizeof v/ sizeof v[0]);
return size;
#endif
}
static void fail(char* s)
{
fprintf(stderr, "check: %s\n", s);
abort();
}
int main(int ac, char** av)
{
int n = 50; /* mallocs in main. */
int n0;
list_t* head;
double begin;
double end;
double t = 2.5e-9;
if (ac > 1)
n = atoi(av[1]);
n0 = n;
head = new_list(NULL);
printf("check starts\n");
begin = sec();
while (n > 0) {
add(head, malloc(nextsize()));
n -= 1;
if ((n & 1) && !empty(head)) {
add_to_free_list(take_out_first(head)); //before free(take_out_first(head))
}
}
printf("Done");
while (!empty(head))
add_to_free_list(take_out_first(head)); //before free(take_out_first(head))
free_list_memory(); //added line
end = sec();
printf("check is ready\n");
printf("total = %1.3lf s\n", end-begin);
printf("m+f = %1.3g s\n", (end-begin)/(2*n0));
printf("cy = %1.3lf s\n", ((end-begin)/(2*n0))/t);
return 0;
}
在valgrind中运行可提供:
check starts
==10011== Invalid read of size 8
==10011== at 0x1000009B8: free_list_memory (check.c:20)
==10011== by 0x100000D8B: main (check.c:163)
==10011== Address 0x10081e270 is 0 bytes inside a block of size 423 free'd
==10011== at 0x10000894F: free (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==10011== by 0x1000009CA: free_list_memory (check.c:21)
==10011== by 0x100000D8B: main (check.c:163)
==10011==
==10011== Invalid free() / delete / delete[] / realloc()
==10011== at 0x10000894F: free (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==10011== by 0x1000009CA: free_list_memory (check.c:21)
==10011== by 0x100000D8B: main (check.c:163)
==10011== Address 0x10081e270 is 0 bytes inside a block of size 423 free'd
==10011== at 0x10000894F: free (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==10011== by 0x1000009CA: free_list_memory (check.c:21)
==10011== by 0x100000D8B: main (check.c:163)
==10011==
==10011==
==10011== More than 10000000 total errors detected. I'm not reporting any more.
==10011== Final error counts will be inaccurate. Go fix your program!
==10011== Rerun with --error-limit=no to disable this cutoff. Note
==10011== that errors may occur in your program without prior warning from
==10011== Valgrind, because errors are no longer being displayed.
==10011==
^C==10011==
==10011== HEAP SUMMARY:
==10011== in use at exit: 38,785 bytes in 423 blocks
==10011== total heap usage: 602 allocs, 6,176,342 frees, 149,153 bytes allocated
==10011==
==10011== LEAK SUMMARY:
==10011== definitely lost: 0 bytes in 0 blocks
==10011== indirectly lost: 0 bytes in 0 blocks
==10011== possibly lost: 0 bytes in 0 blocks
==10011== still reachable: 4,120 bytes in 2 blocks
==10011== suppressed: 34,665 bytes in 421 blocks
==10011== Rerun with --leak-check=full to see details of leaked memory
==10011==
==10011== For counts of detected and suppressed errors, rerun with: -v
==10011== ERROR SUMMARY: 10000000 errors from 2 contexts (suppressed: 0 from 0)
现在,我不明白出了什么问题,因为我正在从使用malloc创建的列表中删除元素…为什么要在自由列表中使用双链接(闭环)列表,如果只在一个方向上运行 您的
void free\u list\u memory(void)
函数在第二次尝试在列表上运行时崩溃
也许这有助于:
void add_to_free_list(list_t* add) {
if (free_list != NULL) {
free_list->succ = add;
add->succ = NULL;
} else {
free_list = add;
add->succ = NULL;
}
}
如果你的自由列表只有一个方向,为什么要使用双链接(闭环)列表呢 您的
void free\u list\u memory(void)
函数在第二次尝试在列表上运行时崩溃
也许这有助于:
void add_to_free_list(list_t* add) {
if (free_list != NULL) {
free_list->succ = add;
add->succ = NULL;
} else {
free_list = add;
add->succ = NULL;
}
}
同样,问题更多的是,代码在到达空闲列表末尾时没有停止,因为它是一个循环列表,终止条件是寻找不存在的空指针
您可以通过将free\u list\u memory()
改写为以下内容来修复大多数问题:
static void free_list_memory(void)
{
if (free_list == 0)
return;
list_t *p = free_list;
do
{
list_t *q = p->succ;
// free(p->data); // Removed: see commentary below!
free(p);
p = q;
} while (p != free_list);
}
通过这种更改,我得到了一个大小为24(64位构建)的未缠绕内存块。因此有一个list\u t
没有被释放。该项目在标题中
;它恰好有一个空数据指针,因此您可以使用以下方法修复最终泄漏:
free(head);
在main()
的末尾
惊喜
我曾经说过:
您还需要释放数据以及列表条目
让我有点惊讶的是,在重新测试时,这是没有必要的。事实上,在我的机器上,在free\u list\u memory()
中,所有的数据指针都是空的。这实际上很不幸-malloc()
正在返回归零数据。实际情况是,列表释放代码释放保存数据的原始list\t
指针,然后将原始数据指针添加到自由列表,将其视为list\t
结构。如果分配的块的大小(来自nextsize()
的随机数)小于列表的大小,则会出现问题
为了解决问题并获得真正干净的运行,即使条目数不同,我创建了这个插入指令的代码版本(WhozCraig的函数处于活动状态)
修订守则
注意,代码确保分配的空间至少与列表\u t
结构一样大;这对于避免内存访问错误是至关重要的(在长时间运行时;我使用200来解决小分配问题,而不添加sizeof(list\t)
,因为前50个分配总是足够大)
瓦尔研磨输出
从打印的指针值可以看出,添加到自由列表的数据实际上是数据,而不是保存数据的list\u t
结构
将整个list\u t
结构从活动列表移动到自由列表,而不是释放list\u t
结构并将数据用作自由列表上的list\u t
结构,这将是更为传统的做法(也可以说是明智的做法)
此外,您可能还应该记录数据的大小,以便知道免费列表中的内容是否可用于特定分配。,问题更多的是,代码在到达空闲列表末尾时没有停止,因为它是一个循环列表,终止条件是查找不存在的空指针
您可以通过将free\u list\u memory()
改写为以下内容来修复大多数问题:
static void free_list_memory(void)
{
if (free_list == 0)
return;
list_t *p = free_list;
do
{
list_t *q = p->succ;
// free(p->data); // Removed: see commentary below!
free(p);
p = q;
} while (p != free_list);
}
通过这种更改,我得到了一个大小为24(64位构建)的未缠绕内存块。因此有一个list\u t
没有被释放。该项目在标题中
;它恰好有一个空数据指针,因此您可以使用以下方法修复最终泄漏:
free(head);
在main()
的末尾
惊喜
我曾经说过:
您还需要释放数据以及列表条目
让我有点惊讶的是,在重新测试时,这是没有必要的。事实上,在我的机器上,在free\u list\u memory()
中,所有的数据指针都是空的。这实际上很不幸-malloc()
正在返回归零数据。实际情况是,列表释放代码释放保存数据的原始list\t
指针,然后将原始数据指针添加到自由列表,将其视为list\t
结构。如果分配的块的大小(来自nextsize()
的随机数)小于列表的大小,则会出现问题
为了解决问题并获得真正干净的运行,即使条目数不同,我创建了这个插入指令的代码版本(WhozCraig的函数处于活动状态)
修订守则
注意,代码确保分配的空间至少与列表\u t
结构一样大;这对于避免内存访问错误是至关重要的(在长时间运行时;我使用200来解决小分配问题,而不添加sizeof(list\t)
,因为前50个分配总是足够大)
瓦尔研磨输出
从打印的指针值可以看出,添加到自由列表的数据实际上是数据,而不是保存数据的list\u t
结构
将整个list\u t
结构从活动列表移动到自由列表,而不是释放list\u t
结构并将数据用作自由列表上的list\u t
结构,这将是更为传统的做法(也可以说是明智的做法)
此外,您可能应该记录数据的大小,以便知道空闲列表中的内容是否可以用于特定的分配。假设您的其余代码是正确的(这是一个很大的假设),您的空闲内存列表函数不能解释列表是正确的这一事实
$ valgrind --leak-check=full --suppressions=suppressions crashing 10
==41722== Memcheck, a memory error detector
==41722== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==41722== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==41722== Command: crashing 10
==41722==
--41722-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--41722-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--41722-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
1: data address is 0x0
check starts
Size: 447
1: data address is 0x10083f3e0
Size: 2825
1: data address is 0x100842440
Size: 3313
1: data address is 0x100842f90
Size: 3138
1: data address is 0x100843cd0
Size: 1946
1: data address is 0x10083f760
Size: 2784
1: data address is 0x100844960
Size: 3824
1: data address is 0x100845480
Size: 2582
1: data address is 0x100846410
Size: 3931
1: data address is 0x100846ed0
Size: 1125
1: data address is 0x100847ed0
Done
2 0x10083f3e0: data is null
2 0x100842440: data is null
2 0x100842f90: data is null
2 0x100843cd0: data is null
2 0x10083f760: data is null
2 0x100844960: data is null
2 0x100845480: data is null
2 0x100846410: data is null
2 0x100846ed0: data is null
2 0x100847ed0: data is null
Check is ready
total = 0.010 s
m+f = 0.000487 s
cy = 194959.641 s
==41722==
==41722== HEAP SUMMARY:
==41722== in use at exit: 39,132 bytes in 430 blocks
==41722== total heap usage: 528 allocs, 98 frees, 71,359 bytes allocated
==41722==
==41722== LEAK SUMMARY:
==41722== definitely lost: 0 bytes in 0 blocks
==41722== indirectly lost: 0 bytes in 0 blocks
==41722== possibly lost: 0 bytes in 0 blocks
==41722== still reachable: 26,034 bytes in 311 blocks
==41722== suppressed: 13,098 bytes in 119 blocks
==41722== Reachable blocks (those to which a pointer was found) are not shown.
==41722== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==41722==
==41722== For counts of detected and suppressed errors, rerun with: -v
==41722== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 15)
$
void free_list_memory(void)
{
if (!free_list)
return;
// terminate "last" node (the node that refers
// back to the node pointed to by free_list,
// including a potential self-referencing node)
free_list->pred->succ = NULL;
// now run the loop. uses free_list as the iterator
// as it will finish with NULL, as it will be empty.
while (free_list)
{
void *p = free_list;
free_list = free_list->succ;
free(p);
}
}