Is it better to use malloc or calloc to allocate memory?

already exists.

Would you like to merge this question into it?

already exists as an alternate of this question.

Would you like to make it the primary and merge this question into it?

exists and is an alternate of .

In general using malloc is faster, since calloc initializes the allocated memory to contain all zeroes. If this is what you want, however, then calloc can be used. The results can vary among different operating systems and environments, though. Memory allocation in an OS that uses floating blocks in heaps, such as Microsoft Windows and MacOS, should use the OS-native memory allocators instead. Answer "Use malloc() almost always and calloc() almost never." The reason is that the initialization to zero that calloc() performs is usually not very helpful: - The initialization to "all-bits-zero" is not necessarily the same as initialization to "all-data-zero." C says very little about the representation of values in memory, nothing at all for floating-point or pointer values. On many machines all-bits-zero representations will in fact correspond to f.p. zeroes or null pointers, but this is not guaranteed by the language and there have been machines where the correspondence did not hold. If you get in the habit of using calloc() to initialize f.p. and pointer items, you may be heading for trouble. - Usually, one allocates a chunk of dynamic memory in order to store something in it -- and when you store something in it, you'll overwrite whatever was there before. Thus, the initialization performed by calloc() is usually not needed anyhow. There are occasional exceptions where all- bits-zero initialization is helpful, but they are unusual.
24 people found this useful

What are the differences between malloc and calloc?

Answer . malloc returns a block of memory that is allocated for the programmer to use, but is uninitialized. The memory is usually initialized by hand if necessary -- eith

Difference between malloc and calloc?

malloc is used to allocate memory for one variable int* hi = (int*)malloc(sizeof(int)); //allocates the amount of memory an integer needs and casts it to an integer (defaul

Use of malloc and calloc?

Answer . The use of malloc and calloc is dynamic memory allocation. malloc allocates memory in bytes whereas calloc allocates memory in blocks. calloc initializes the

What is malloc vs calloc?

The use of malloc and calloc is dynamic memory allocation. malloc allocates memory in bytes whereas calloc allocates memory in blocks. calloc initializes the allocated

What are the advantages of malloc and calloc?

malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available.calloc(...) allocates an array in memo