The difference and usage of calloc, malloc and realloc functions

Time:2021-2-19
/*The memory size needs to be calculated manually and is not initialized*/
int *p  = (int *)malloc(20 * sizeof(int));
/*There is no need to calculate the memory size manually, and it is initialized to 0*/
int *q  = (int *)calloc(20, sizeof(int));
/*Dynamic memory expansion / reduction * 1. Successful allocation: 
 *1.1 if the return address is the same and there is spare memory after the original memory, return the address of the original pointer;
 *1.2 if the return address is different and there is no spare memory after the original memory, allocate a new memory and copy the original memory content to free the original memory; * 2
 *2.1 if the original memory is not released, if the reallocated memory is too large, the allocation fails and null is returned, the original memory is not released;
 *2.1 release the original memory, the original pointer is not null, the size of reallocation is 0, return null, release the original memory, realloc (P, 0) = = free (P); don't use it like this! * 3. The expanded memory is smaller than the original, and the data will be lost * / int * PP = (int *) realloc (P, (20 + 10) * sizeof (int));