C - calloc() v.malloc()
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自 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, sincecalloc()
initializes the allocated memory to contain all zeros. Since you typically would want to use and initialize the memory yourself, this additional benefit ofcalloc()
may not be necessary.calloc
会在使用内存之前对其进行初始化,但malloc
不会。请参阅此链接:
使用
malloc
,如果你想保证相同的效果,你必须调用 memset 之类的东西来重置内存,例如calloc
可以节省你的时间额外的步骤。初始化内存的重要性在于,您正在将变量置于已知状态而不是未知状态。因此,如果您正在检查一个变量(例如数组元素)的预期值,那么通过提前预初始化该变量,您可以确保您正在检查的值不是垃圾。 换句话说,您可以区分垃圾值和合法值。例如
,如果您只是将垃圾值留在变量中,并且正在检查某个值(例如 42),那么您无法知道是否存在该值确实被您的程序设置为 42,或者这只是因为您没有初始化它而留下的一些垃圾。
calloc
is initializing the memory before you use it, butmalloc
does not.Refer to this link:
With
malloc
, if you want to guarantee the same effect you'd have to call something like memset to reset the memory, e.g.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.
calloc(...)
基本上是malloc
+memset
(如果你想 0 初始化内存)当你使用
malloc< /code> 分配一些内存,它以前的内容没有被清除(即没有初始化)。您可能会获得机器启动时设置的随机值,或者您可能会看到一些属于先前运行的程序但在分配和程序退出后未被清除的内存。
calloc
本身比malloc
慢,因为您必须花费一些时间来清除分配的内存内容。因此,如果您只需要分配一些内存,然后在那里复制一些内容,则可以自由使用malloc
。calloc(...)
is basicallymalloc
+memset
(if you want to 0 initialise the memory)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 thanmalloc
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 usemalloc
.它只是意味着,如果您使用 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.