C language malloc function
malloc() dynamically allocates memory storage space
Header file : #include<stdlib.h>
Prototype : void *malloc(size_t size)
Parameter description : size
The size of the memory space to be allocated, the unit is Byte
Function description : Allocate a block of memory space in the heap area size Byte
to store data. After this memory space is allocated, it will not be initialized, so The values corresponding to these memory spaces are unknown. If you need to initialize the allocated memory, you can use the calloc()
function
return value : the allocation is successful and returns a pointer to the allocated memory. Note that although the return value type of the function is void *
, void
it does not mean that there is no return value or a null pointer, but the return pointer type Unknown, so you malloc()
often have to use casts when using functions
char *pr = (char *)malloc(10);
calloc() allocates memory space and initializes it
Header file : #include<stdlib.h>
Prototype : void *calloc(size_t size, size_t num);
Parameter description : num
The number of spaces that need to be allocated continuously, size
the size of each allocated space, in Bytes.
Function description : Dynamically allocate num
a continuous memory space with a length of size Byte
0 in memory, and initialize each byte to 0.
Return value : If the allocation is successful, it returns the address pointing to the memory. If it fails, it returns NULL. The same malloc()
as the function, the return value type of the function is void *
.