C语言 C - calloc() 诉 malloc()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3449031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C - calloc() v. malloc()
提问by Kevin Meredith
Possible Duplicate:
c difference between malloc and calloc
可能的重复:
c malloc 和 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.
malloc() 和 calloc() 函数之间的另一个区别是 malloc() 函数分配的内存包含垃圾值,而 calloc() 函数分配的内存包含全零。
Source ('C' Programming, Salim Y. Amdani)
来源(“C”编程,Salim Y. Amdani)
Thanks
谢谢
回答by Edward Leno
From http://wiki.answers.com/Q/Is_it_better_to_use_malloc_or_calloc_to_allocate_memory
来自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.
malloc()更快,因为calloc()初始化分配的内存以包含所有零。由于您通常希望自己使用和初始化内存,因此calloc()可能不需要这种额外的好处。
回答by dcp
callocis initializing the memory before you use it, but mallocdoes not.
calloc在您使用它之前初始化内存,但malloc没有。
Refer to thislink:
参考这个链接:
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.
calloc() 函数应为每个以字节为单位的大小为 elsize 的 nelem 元素数组分配未使用的空间。空间应初始化为所有位 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.
使用malloc,如果你想保证同样的效果,你必须调用类似 memset 的东西来重置内存,例如
char* buffer = (char*)malloc(100);
memset(buffer,0,100);
callocsaves 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.
calloc为您节省了额外的步骤。初始化内存的意义在于您将变量置于已知状态而不是未知状态。因此,如果您正在检查一个变量,例如一个数组元素,以获得预期值,那么通过提前预初始化该变量,您可以确保您正在检查的值不是垃圾。换句话说,您可以区分垃圾值和合法值。
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.
例如,如果你只是在变量中留下垃圾并且你正在检查某个值,比如 42,那么你无法知道该值是否真的被你的程序设置为 42,或者这只是一些垃圾剩余,因为你没有初始化它。
回答by plaes
calloc(...)is basically malloc+ memset(if you want to 0 initialise the memory)
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.
When you use mallocto 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.
当你malloc用来分配一些内存时,它之前的内容不会被清除(即没有被初始化)。您可能会获得机器启动时设置的随机值,或者您可能会看到一些属于以前运行的程序但在分配和程序退出后未被清除的内存。
callocitself is slower than mallocbecause 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.
calloc本身比malloc因为你必须花一些时间来清除分配内存的内容要慢。所以如果你只需要分配一些内存然后在那里复制一些东西,你可以自由地使用malloc.
回答by Shawn D.
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.
这只是意味着,如果您使用 calloc() 分配内存,则分配的任何内容都是 0。即,如果您为整数数组分配空间,则它们都将设置为 0,而使用 malloc(),则没有'没有以任何方式初始化。
You could use calloc in situations where you are just going to do a memset of 0 to the memory anyway.
您可以在无论如何都要对内存执行 0 的 memset 的情况下使用 calloc。

