First, under the GCC compiler, let's look at the functions of these functions:
From this we can see that:
function malloc ()
Allocates a contiguous region of size bytes in the dynamic storage area of memory, the parameter size is the length of the desired memory space, and returns the first address of the range;
function Calloc ()
The parameter size is the cell element length of the request address, and NMEMB is the number of elements, that is, the contiguous address space of the NMEMB *size byte size is requested in memory;
function ReAlloc ()
To redistribute the space to a pointer that has already been assigned an address, the parameter PTR is the original space address, and the size is the address length of the RE application;
Here is a specific discussion of the use of the three:
Both malloc and Calloc,realloc can be used to dynamically allocate memory space, but there are some differences between them:
The 1.Malloc function has one parameter, that is, the size of the memory space to allocate
The 2.calloc has two parameters, 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
3. If the allocated memory space succeeds, both returns the program beginning of the first address function of the allocated memory space
4.malloc cannot initialize the allocated memory space, and Calloc can
5. Since the memory space allocated by malloc has not been used, each of these may be zeros, and conversely, if this part of memory has been used, there may be a wide variety of data in it, that is, the use of malloc (memory space has not been reassigned) to function correctly, However, a problem may occur over time (memory space has been reassigned).
6. Function Calloc initializes each digit in the allocated memory space to 0. That is, if you allocate memory space for elements of character or data type, these elements will be initialized to 0, and if you are allocating memory space for elements of the pointer type, these elements will be initialized to null pointers. If you allocate memory for real data, these elements are initialized to 0 of the floating-point type.
The 7.Realloc is a reallocation of space to an address pointer that has been allocated memory, including the spatial address of the reason and the length of the address to be applied again.
The substance of the malloc function now, it has a so-called idle list that connects the available chunks of memory to a long listing. When the malloc function is invoked, it looks for a block of memory that is large enough to meet the user's request in the join table. The memory block is then split in half (the size of the piece is equal to the size of the user request, and the other is the remaining byte). Next, the memory assigned to the user is passed to the user, and the remaining piece, if any, is returned to the connection table. When the free function is invoked, it connects the memory blocks freed by the user to the idle chain. In the end, the free chain will be cut into a lot of small memory fragments, if the user requests a large memory fragment, then the idle chain may not be able to meet the user requirements of the fragment. As a result, the MALLOC function requests a delay and begins to rummage through the free chain to check each memory fragment, organize them, and merge the adjacent small free blocks into larger chunks of memory.