简体   繁体   English

calloc 问题 - malloc.c:2385 错误 - calloc/malloc 如何工作?

[英]calloc problem - malloc.c:2385 error - how does calloc/malloc work?

When running my program I receive following error:运行我的程序时,我收到以下错误:

malloc.c:2385: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || malloc.c:2385: sysmalloc: 断言`(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' 失败。

The complete code is huge, but I have managed to find something interesting in reproduction, and I cannot understand why this is happening.完整的代码很大,但我设法在复制中找到了一些有趣的东西,我不明白为什么会这样。 Any help and/or explanation would be very much appreciated.任何帮助和/或解释将不胜感激。

Here is a snippet of the code when it is not failing:这是没有失败时的代码片段:

static void adjacent_list_get(struct elem **elem_list,
                          struct elem *element,
                          uint32_t *size) {

struct elem **t_elem_list;

t_elem_list = calloc((size_t) 1, sizeof(*t_elem_list));     

if (!elem_list) {       
...

And here is a snippet of the code when it fails:这是失败时的代码片段:

static void adjacent_list_get(struct elem **elem_list,
                          struct elem *element,
                          uint32_t *size) {

struct elem **t_elem_list;

if (!elem_list) {   
        t_elem_list = calloc((size_t) 1, sizeof(*t_elem_list)); 
        ...

During execution, elem_list is NULL , so the if statement is true and it enters that block;在执行过程中, elem_listNULL ,所以if语句为真,进入该块; but then it fails with error mentioned above.但随后它因上述错误而失败。

I cannot understand how the same line, same calloc can fail in the same function depending on if it is inside or out of the if block.我无法理解同一行,同一calloc如何在同一 function 中失败,具体取决于它是在if块内部还是外部。 I am missing something here, and I could not find any explanation on the internet.我在这里遗漏了一些东西,我在互联网上找不到任何解释。

how does calloc/malloc work? calloc/malloc 是如何工作的?

It depends a lot of your implementation.这在很大程度上取决于您的实施。

Some implementations of calloc/malloc are open source: try installing Debian on your laptop, study the source code of GNU libc or musl-libc , and read Linux From Scratch . calloc/malloc的一些实现是开源的:尝试在笔记本电脑上安装Debian ,研究GNU libcmusl-libc的源代码,并阅读Linux From Scratch Then malloc would sometimes use system calls like mmap(2) or sbrk(2) .然后malloc有时会使用系统调用,如mmap(2)sbrk(2) Most of the time malloc would reuse previously free d memory zones.大多数时候malloc会重用以前free的 d memory 区域。

See also this answer for a joke-implementation of malloc which I believe is conforming to some C standard (eg n1570 or better).另请参阅此答案,了解malloc的笑话实现,我认为它符合某些 C 标准(例如n1570或更好)。

On the other hand, reading the source code of malloc inside Windows is reserved to a very few happy fews.另一方面,阅读malloc内部的 malloc 的源代码是为少数快乐的人保留的。

On microcontrollers like Arduino , malloc works very differently.在像Arduino这样的微控制器上, malloc工作方式非常不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM