首页 文章

已分配的内存块上的calloc是否会调用重复分配? [等候接听]

提问于
浏览
1

我已经为1024 char 元素分配了一块内存 . *p 指向其第一个地址 .
现在我想将所有值设置为零 . 如果我使用
p = (char *) calloc(1024, sizeof(char))
它会重用已经分配的内存块,还是会在其他地方分配1024个新字节并更改 *p 指向的地址?如果发生了这种情况,那么旧街区会发生什么?因为我真的不能再打电话给 free() 了 .

1 回答

  • 2

    已分配的内存块上的calloc是否会调用重复分配?

    不,不是的 . 你丢失了旧的记忆位置 .

    它会重用已分配的内存块还是会分配1024个新字节?

    再一次,不,它不会 . 你得到一个新的记忆位置 .

    旧街区会发生什么?

    会有内存泄漏,因为无法再次访问它 .

    我实际上不能再调用free()了 .

    你就在这里

    手册页说:

    DESCRIPTION
           operation is performed.
    
           The calloc() function allocates memory for an array of nmemb elements of size bytes  each
           and  returns  a pointer to the allocated memory.  The memory is set to zero.  If nmemb or
           size is 0, then calloc() returns either NULL, or a unique pointer value that can later be
           successfully passed to free().
    
    
    RETURN VALUE
           The malloc() and calloc() functions return a pointer to the allocated  memory,  which  is
           suitably aligned for any built-in type.  On error, these functions return NULL.  NULL may
           also be returned by a successful call to malloc() with a size of zero, or by a successful
           call to calloc() with nmemb or size equal to zero.
    

相关问题