The generation and release of heap space in C language
- 2020-04-02 01:16:08
- OfStack
Allocation and release of heap space
# include < Stdlib. H >
Malloc, calloc, realloc, free
The malloc
Void * malloc (size_t size);
Function: allocate size bytes of contiguous space in the heap
Parameter: size_ number of bytes
Return value: returns the first address of the allocated space on success and NULL on failure
free
Void free (void * PTR);
Function: free space allocated by malloc, calloc, realloc
Parameter: the first address of the ptr_ space
Return value: none
Note:
1. Each space can only be released once
2. PTR must be the first address of the allocated space
calloc
Void *calloc(size_t nmemb, size_t size);
Function: allocate contiguous space in the heap with nmemb block size of size bytes
Parameter: nmemb_ data block size size_ each size
Return value: returns the first address of the allocated space on success and NULL on failure
Note: Calloc sets the space content to zero, whereas malloc does not
realloc
Void *realloc(void * PTR, size_t size);
Function: in malloc, calloc, realloc allocated PTR start space, reassign to size byte size
Parameters: ptr_malloc, calloc, realloc allocate space first address size_ total size
Return value: successfully returns the first address of the allocated space and fails to return NULL
Note:
1. If the size is less than the original space size, it will not work
2. The newly added space will not set 0
3. If PTR is NULL, it is equivalent to malloc(size).
4. If PTR is not NULL and size == 0, it is equivalent to free(PTR).
5. PTR does not need to be released. If the allocation is successful, only the first address of the redistribution space is released
# include < Stdlib. H >
Malloc, calloc, realloc, free
The malloc
Void * malloc (size_t size);
Function: allocate size bytes of contiguous space in the heap
Parameter: size_ number of bytes
Return value: returns the first address of the allocated space on success and NULL on failure
free
Void free (void * PTR);
Function: free space allocated by malloc, calloc, realloc
Parameter: the first address of the ptr_ space
Return value: none
Note:
1. Each space can only be released once
2. PTR must be the first address of the allocated space
calloc
Void *calloc(size_t nmemb, size_t size);
Function: allocate contiguous space in the heap with nmemb block size of size bytes
Parameter: nmemb_ data block size size_ each size
Return value: returns the first address of the allocated space on success and NULL on failure
Note: Calloc sets the space content to zero, whereas malloc does not
realloc
Void *realloc(void * PTR, size_t size);
Function: in malloc, calloc, realloc allocated PTR start space, reassign to size byte size
Parameters: ptr_malloc, calloc, realloc allocate space first address size_ total size
Return value: successfully returns the first address of the allocated space and fails to return NULL
Note:
1. If the size is less than the original space size, it will not work
2. The newly added space will not set 0
3. If PTR is NULL, it is equivalent to malloc(size).
4. If PTR is not NULL and size == 0, it is equivalent to free(PTR).
5. PTR does not need to be released. If the allocation is successful, only the first address of the redistribution space is released