如何正确重新分配 calloc?

发布于 2025-01-17 04:20:02 字数 1023 浏览 0 评论 0原文

有人可以告诉我如何使用 realloc 来正确扩展 calloc 吗?

我的期望: 所有 20 个插槽的值为 0。

结果: 只有 calloc 插槽的值为 0,我用 realloc 分配的插槽具有随机数。 输出图片

#include <stdio.h>
#include <stdlib.h>
int main(){
    int *ptr;
    int i;
    int n =10;
    ptr = (int*)calloc(n, sizeof(int));    //first time allocate with calloc
    ptr = (int*)realloc(ptr,2*n*sizeof(int));  //extend with realloc
    
    for(i=0;i<2*n;i++){
        printf("ptr[%d]: %d \n", i, ptr[i]);
    }
    return 0;
}

注意:在有人问我为什么不直接使用calloc(20,sizeof(int));。这不是我正在编写的真正程序,这只是一个简短的演示,以便人们可以更容易地看到我的问题。我实际上打算在程序的后期扩展 calloc 。

在创建此问题之前我尝试找到答案。我谷歌了这个:

  • 如何正确重新分配?
  • 如何扩展Calloc?
  • 如何将 realloc 与 calloc 一起使用?
  • calloc() 和 realloc() 编译会
  • 出现几个与上面类似的问题。

但我似乎找不到真正的答案(我可能错过了,我真的很抱歉)。我得到的大多数答案是他们像我一样使用 realloc 来扩展 calloc,但这正是我的问题。抱歉英语不好。

can someone tell me how to use realloc to properly extend calloc?

my expectation:
all 20slots to have value of 0.

Outcome: only calloc slots have value of 0, the one I allocate with realloc have random number. output picture

#include <stdio.h>
#include <stdlib.h>
int main(){
    int *ptr;
    int i;
    int n =10;
    ptr = (int*)calloc(n, sizeof(int));    //first time allocate with calloc
    ptr = (int*)realloc(ptr,2*n*sizeof(int));  //extend with realloc
    
    for(i=0;i<2*n;i++){
        printf("ptr[%d]: %d \n", i, ptr[i]);
    }
    return 0;
}

NOTE: before anyone ask why don't I just use calloc(20,sizeof(int));. This is not the real program that I am writing, this is just a short demo so people can see my problem easier. I actually intent to extend calloc much later in the program.

My attempt to find the answer before creating this question. I had google this:

  • How to realloc properly?
  • How to extend Calloc?
  • How to use realloc with calloc?
  • calloc() and realloc() compiling issue
  • several similar to the above.

But I seem to can't find the real answer to this(I might missed it, I'm really sorry). Most of the answer that I got is that they use realloc to extend calloc just like I did, but that is exactly my problem. sorry for bad English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌生 2025-01-24 04:20:02

C 标准不提供任何执行重新分配并将附加内存清零的例程。为了实现这一点,您可以通过使用 realloc 重新分配内存并使用 memset 清除来自己实现它:

int *newptr = realloc(ptr, 2 * n * sizeof *newptr);
if (!newptr)
{
    fprintf(stderr, "Error, unable to allocate memory.\n");
    exit(EXIT_FAILURE);
}
ptr = newptr;
memset(ptr + n, 0, n * sizeof *ptr);
n *= 2;

您当然可以将其包装到一个例程中,比如一个名为 crealloc,然后使用它。 (与内存管理的库函数不同,它必须传递先前分配的大小,因为您编写的代码通常无法访问提供该大小的内存管理数据结构。)

The C standard does not provide any routine that performs a reallocation and zeros the additional memory. To achieve this, you can implement it yourself by using realloc to reallocate memory and clearing with memset:

int *newptr = realloc(ptr, 2 * n * sizeof *newptr);
if (!newptr)
{
    fprintf(stderr, "Error, unable to allocate memory.\n");
    exit(EXIT_FAILURE);
}
ptr = newptr;
memset(ptr + n, 0, n * sizeof *ptr);
n *= 2;

You could of course wrap this into a routine, say one called crealloc, and use that. (Unlike the library functions for memory management, it would have to be passed the size of the prior allocation, since code you write does not ordinarily have access to the memory management data structures that provide that size.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文