memset

Memset (void p, int c, int n) Initializes the requested space and initializes the n-byte space pointed to by P to C in bytes, for example, char *p = (char *)malloc(sizeof(char) * 100);

Memset (&p, 0, sizeof(char) * 100) memset(&p, 0, sizeof(char) * 100) memset(&p, 0, sizeof(char) * 100)

malloc

Void * malloc(int n) Passes in the requested space size, in bytes, and returns the memory address of the control

A memset(void *p, int c, int n) is used to initialize the memory space

The memset is used to initialize the requested space. Otherwise, the requested space is the default non-blank space in memory, which may be garbled or different from the expected value

Note: The space allocated by the malloc function is also uninitialized.

calloc

The calloc() function is a simple wrapper around malloc. Its main advantage is to zero out dynamically allocated memory.

void *calloc(int n, int size)

One notable difference between this function and malloc is that the calloc function gets initialized memory with zero contents. The calloc function is a good way to apply space to an array. You can set size to the size of the array elements and n to the size of the array.

Note: The space allocated by relloc is also initialized.

realloc

Realloc functions are more functional than malloc and calloc functions, which can realize the function of memory allocation and memory release. Its function declaration is as follows:

void * realloc(void * p,int n);

Where, the pointer P must be a pointer to the heap memory space, that is, the space allocated by the malloc, calloc, or realloc functions. The realloc function changes the size of the memory block pointed to by the pointer P to n bytes. If n is less than or equal to the size of the space that P was pointing to, then. Keep the original state unchanged. If n is larger than the size of the space previously pointed to by P, the system will re-allocate a chunk of memory space with size N from the heap for P. Meanwhile, the contents of the original pointed space will be copied to the new memory space, and the space previously pointed by P will be released.

Note: the space allocated by relloc function is also uninitialized. It will be reallocated according to the newly specified size and the original size. If the size is smaller than the address, it will be copied and reallocated.

free

void free(void * p); Free the space requested by p and set it to null