------------恢复内容开始------------
realloc()
Malloc and Calloc
Initialization: malloc() allocates memory block of given size (in bytes) and returns a pointer to the beginning of the block. malloc() doesn’t initialize the allocated memory. If we try to access the content of memory block(before initializing) then we’ll get segmentation fault error(or maybe garbage values).
calloc() allocates the memory and also initializes the allocated memory block to zero. If we try to access the content of these blocks then we’ll get 0.
The pointer returned by malloc
isn‘t backed by real memory until the program actually touches it.
calloc
does indeed touch the memory (it writes zeroes on it) and thus you‘ll be sure the OS is backing the allocation with actual RAM (or swap). This is also why it is slower than malloc (not only does it have to zero it, the OS must also find a suitable memory area by possibly swapping out other processes)
原文:https://www.cnblogs.com/anyu686/p/12977978.html