C - calloc() v.malloc()

发布于 2024-09-13 11:41:33 字数 407 浏览 4 评论 0原文

可能的重复:
malloc 和 calloc 之间的区别

请解释一下这句话的意义,

另一个 malloc() 和 malloc() 之间的区别 calloc() 函数的作用是内存 通过malloc()函数分配 包含垃圾值,而内存 由calloc()函数分配 全部包含零。

来源('C' 编程,Salim Y. Amdani)

谢谢

Possible Duplicate:
c difference between malloc and calloc

Please explain the significance of this statement,

Another
difference between the malloc() and
calloc() functions is that the memory
allocated by malloc( ) function
contains garbage values, while memory
allocated by calloc( ) function
contains all zeros.

Source ('C' Programming, Salim Y. Amdani)

Thanks

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

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

发布评论

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

评论(4

心是晴朗的。 2024-09-20 11:41:33

来自 http://wiki.answers.com/Q/Is_it_better_to_use_malloc_or_calloc_to_allocate_memory

malloc()< /code> 更快,因为 calloc() 初始化分配的内存以包含全零。由于您通常希望自己使用和初始化内存,因此 calloc() 的这一额外优势可能不是必需的。

From http://wiki.answers.com/Q/Is_it_better_to_use_malloc_or_calloc_to_allocate_memory

malloc() is faster, since calloc() initializes the allocated memory to contain all zeros. Since you typically would want to use and initialize the memory yourself, this additional benefit of calloc() may not be necessary.

儭儭莪哋寶赑 2024-09-20 11:41:33

calloc 会在使用内存之前对其进行初始化,但 malloc 不会。

请参阅链接:

calloc()函数应分配
nelem 数组的未使用空间
每个元素的大小以字节为单位
是埃尔大小。该空间应为
初始化为所有位 0。

使用 malloc,如果你想保证相同的效果,你必须调用 memset 之类的东西来重置内存,例如

char* buffer = (char*)malloc(100);
memset(buffer,0,100);

calloc 可以节省你的时间额外的步骤。
初始化内存的重要性在于,您正在将变量置于已知状态而不是未知状态。因此,如果您正在检查一个变量(例如数组元素)的预期值,那么通过提前预初始化该变量,您可以确保您正在检查的值不是垃圾。 换句话说,您可以区分垃圾值和合法值。例如

,如果您只是将垃圾值留在变量中,并且正在检查某个值(例如 42),那么您无法知道是否存在该值确实被您的程序设置为 42,或者这只是因为您没有初始化它而留下的一些垃圾。

calloc is initializing the memory before you use it, but malloc does not.

Refer to this link:

The calloc() function shall allocate
unused space for an array of nelem
elements each of whose size in bytes
is elsize. The space shall be
initialized to all bits 0.

With malloc, if you want to guarantee the same effect you'd have to call something like memset to reset the memory, e.g.

char* buffer = (char*)malloc(100);
memset(buffer,0,100);

calloc saves you that extra step.
The significance of initializing memory is that you are getting a variable to a known state rather than an unknown one. So if you are checking a variable, say an array element, for an expected value, then by having pre-initialized the variable ahead of time, you can be sure that the value you are checking isn't garbage. In other words, you can distinguish between garbage and legitimate values.

For example, if you just leave garbage in the variable and you are checking for some value, say 42, then you have no way of knowing if the value was really set to 42 by your program, or if that's just some garbage leftover because you didn't initialize it.

萌化 2024-09-20 11:41:33

calloc(...) 基本上是 malloc + memset(如果你想 0 初始化内存)

ptr = malloc(sizeof(struct fubar));
memset(ptr, 0, sizeof (struct fubar)); //here we could use some different value instead of 0 whereas calloc always 0 initialises.

当你使用 malloc< /code> 分配一些内存,它以前的内容没有被清除(即没有初始化)。您可能会获得机器启动时设置的随机值,或者您可能会看到一些属于先前运行的程序但在分配和程序退出后未被清除的内存。

calloc 本身比 malloc 慢,因为您必须花费一些时间来清除分配的内存内容。因此,如果您只需要分配一些内存,然后在那里复制一些内容,则可以自由使用malloc

calloc(...) is basically malloc + memset(if you want to 0 initialise the memory)

ptr = malloc(sizeof(struct fubar));
memset(ptr, 0, sizeof (struct fubar)); //here we could use some different value instead of 0 whereas calloc always 0 initialises.

When you use malloc to allocate some memory, it's previous contents are not cleared (ie not initialized). You might get random values that were set when machine booted up or you might see some of the memory that belonged to previously running programs but was left uncleared after allocation and program exit.

calloc itself is slower than malloc because you have to spend some time to clear the contents of allocated memory. So if you just need to allocate some memory and then copy some stuff there, you are free to use malloc.

夏日落 2024-09-20 11:41:33

它只是意味着,如果您使用 calloc() 分配内存,则分配的任何内容都是 0。即,如果您为整数数组分配空间,它们都会被设置为 0,而使用 malloc() 时,那里的内存不会没有以任何方式初始化。

在您只想对内存执行 0 的 memset 的情况下,您可以使用 calloc。

It just means that if you allocate memory, with calloc(), whatever you allocate is 0. i.e. if you allocated space for an array of integers, they'd all be set to 0, whereas with malloc(), the memory there isn't initialized in any way.

You could use calloc in situations where you are just going to do a memset of 0 to the memory anyway.

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