Several memory request functions commonly used in C language:
void* malloc (unsigned size);
void* realloc (void* ptr, unsigned newsize);
void* calloc (size_t numelements, size_t sizeofelement);
The above functions are defined in the header file Stdlib.h, so you must import the header file when you use it.
malloc:
The malloc function is the most commonly used in our usual writing code. The size of the parameter is the length of memory that needs to be requested, in bytes, or null if the request fails, or returns the first address of the request to the contiguous block of memory, which requires a pointer to the returned first address pointer to be cast to the target type. Such as:
Char *p = (char*) malloc (10);
realloc:
ReAlloc is the reallocation of memory to a pointer that has already been requested, that is, when a pointer to a block of memory is insufficient or redundant, you can use realloc to reallocate the appropriate size of memory, relative to the size of the memory block can be adjusted as needed. The parameter ptr is the original space address, and the newsize is the length of the address that is being applied again.
Such as:
Char *p = (char *) malloc (a);
p = realloc (P, 20);
Eventually p points to an address that may or may not change, but the size of the memory block that P points to is larger, and the contents of the original memory block of P are unchanged.
(the address that P points to may change after the memory is reassigned. Change is based on the actual situation, if the reallocation of memory than the original, and in memory before the original memory block followed by sufficient free space to allocate, then P's first address unchanged, If there is not enough space on the back of the original memory block to allocate, the system will look for a large enough free space elsewhere to allocate p, in which case the address that P points to will change, because either malloc or realloc, The block of memory it requests must be contiguous.
calloc:
Calloc is similar to malloc, but calloc more than malloc to do one step, is to apply to the memory block all initialized to 0. The parameter numelements represents the number of elements, sizeofelement represents the size of each element, that is, the size of the memory block the CALLOC requested is numelements * size_t bytes.
The above functions are all from the heap to apply for memory, so need to program apes themselves free memory, free memory:
Free (VOID*PTR);