C language malloc function

C language malloc function

malloc() dynamically allocates memory storage space

Header file : #include<stdlib.h>
Prototype : void *malloc(size_t size)
Parameter description : sizeThe 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 Byteto 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 *, voidit 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 : numThe number of spaces that need to be allocated continuously, sizethe size of each allocated space, in Bytes.
Function description : Dynamically allocate numa continuous memory space with a length of size Byte0 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 *.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325116493&siteId=291194637