malloc function
(1) Explaining the role of malloc function
The full name of malloc is the memory allocation, which is called dynamic RAM allocation in Chinese.
The malloc function is a memory space that you want the system to request to allocate a specified size byte. The return type of malloc is the void* type. The void* is expressed as a pointer to the type that determines. C + + Specifies that the void* type can be cast to any other type of pointer.
(2) Full name
void * malloc (size_t size);
(3) prototypes
extern void *malloc (size_t size);
(4) header file
#include <stdlib.h>
(5) Return value
If the assignment succeeds, it returns a pointer to the allocated memory, which is assigned the first address of the memory space, otherwise the null pointer null is returned. When the memory is no longer in use, use the free () function to release the memory block, or a memory leak may occur. also, when calling the malloc function to dynamically allocate memory, be aware of its return value.
(6) Implementation mechanism
The malloc function finds the required block of memory in a memory block-free list.
When the malloc function is called, the memory management module finds a block of memory in the idle list that satisfies the user's request, based on the correlation algorithm. The memory block is divided in two (the size of a piece is equal to the size of the user request, the other is the remaining byte), the block assigned to the user is passed to the user, and the remainder is re-hung to the linked list. When you call the free function, it connects the user-freed block of memory to the idle list. Finally, the memory block may be divided into many small blocks of memory, when the user requests a large segment of memory, the idle chain may not meet the needs of the user's memory block, memory needs to be organized, the adjacent small block of memory into a large chunk, there may be malloc function call delay. If the required memory block is not finally found, the malloc function returns a null pointer, so it is important to judge the return value when calling malloc dynamic request memory.
(7) Illustrative examples
int* p = (int *) malloc (sizeof (int));
First, the malloc function returns the void * type if you write: p = malloc (sizeof (int)); The program cannot compile, error: "Cannot assign void* to int * type variable". Therefore, the cast must be passed (int *).
Second, the argument to the function is sizeof (int), which indicates the size required for an integral type of data. If you write:
int* p = (int *) malloc (1);
Code can also be compiled, but in fact only allocates 1 bytes of memory space, when you put an integer inside, there will be 3 bytes homeless, and directly "live in the neighbor's house"! The result is that the contents of the original data in the back memory are all emptied.
(8) the function malloc () and calloc () can be used to dynamically allocate memory space, but the two are slightly different.
The malloc () function has a parameter that is the size of the memory space to allocate:
void *malloc (size_t size);
The Calloc () function has two parameters, each of which is the number of elements and the size of each element, and the product of these two parameters is the size of the memory space to allocate.
void *calloc (size_t numelements,size_t sizeofelement);
If the call succeeds, the function malloc () and function calloc () will return the first address of the allocated memory space.
The main difference between the function malloc () and the function calloc () is that the former cannot initialize the allocated memory space, while the latter can.
If the memory space allocated by the malloc () function has not been used, then each of the bits may be 0, whereas if this part of the memory has been allocated, there may be a variety of data left. that is, when a program that uses the malloc () function starts (the memory space has not been reassigned), it can work, but after a while (the memory space has been reassigned) there may be a problem.
The function calloc () initializes each bit in the allocated memory space to 0 , that is, if you are allocating memory for an element of a character type or integer type, then those elements will be guaranteed to be initialized to 0, and if you are allocating memory for an element of pointer type, These elements are usually initialized to null pointers, and if you allocate memory for real data, those elements are initialized to 0 of the floating-point type.
Introduction to malloc function in C language