There are three common functions to dynamically allocate memory:
1.malloc
2.calloc
3realloc
malloc
void* malloc (size_t size); size is the size of the memory block in bytes.
int main() { int* p = (int*)malloc(40); if (p == NULL) { printf("%s\n", strerror(errno));//print error message return 1; } //Store 1-10 int i = 0; for (i = 0; i < 10; i++) { *(p + i) = i + 1; } //Print for (i = 0; i < 10; i++) { printf("%d ", *(p + i)); } //Release the requested memory free(p); p = NULL; return 0; }
Among them, free is a function used to release the opened dynamic memory. Here, after freeing the pointer memory through the free() function, the pointer is emptied, so as to avoid the program crash caused by the judgment of the illegality of the pointer in the subsequent program. . To release the space, the value of the pointer has not changed. It is impossible to judge whether the space has been released directly through the pointer itself. Setting the pointer to empty helps to judge that the space pointed to by a pointer has been released.
Moreover, the dynamic pointer is maintained on the heap. After the dynamic pointer is defined, the program allocates a piece of memory for the pointer, and ensures that the memory will not be modified by other messy things. When free, it is to give up the occupation of the memory by the pointer. After giving up, the value of the memory will be rewritten into a random value. But the pointer itself is not deleted! The pointer still points to the original memory!
There are a few things to keep in mind when using free:
if parameter ptr The space pointed to is not dynamically opened up, then free The behavior of the function is undefined. if parameter ptr yes NULL pointer, the function is invalid free The function declaration is in<stdlib.h>header file
Precautions for using malloc
If the allocation is successful, a pointer to the allocated space is returned. If opening fails, returns a NULL pointer, so malloc The return value must be checked. The type of the return value is void* ,so malloc The function does not know the type of space to open up, and the user can decide by himself when using it. if parameter size is 0, malloc The behavior of is undefined and depends on the compiler. malloc The function declaration is in<stdlib.h>header file
calloc
void* calloc (size_t num, size_t size); num is the number of elements to allocate, size is the size of each element, size_t is an unsigned integer.
int main() { int* p = (int*)calloc(10, sizeof(int)); if (p == NULL) { perror("calloc");//print error message return 1; } //use int i = 0; for (i = 0; i < 10; i++) { printf("%d ", *(p + i)); } free(p); p = NULL; return 0; }
The functions and usages of malloc and calloc are similar, and they have the following differences
The parameters are different; malloc If the requested space is not initialized, it will directly return to the starting address. calloc After applying for space, it will initialize the space to 0 and then return to the starting address.
Some people may have questions here: since calloc does not need to calculate space and can directly initialize memory to avoid errors, why not just use the calloc function, that is, why use the malloc function?
In fact, the reason is efficiency. Since the calloc function must initialize a value for each space, it must be less efficient than malloc, and there are many times when space applications do not require an initial value, which is why these two functions are used: each has its own advantages.
realloc
void* realloc (void* ptr, size_t size); ptr is a pointer to the original space address, size For the next need to expand the size of the capacity.
The realloc function is essentially different from the above two, and it is used to expand the dynamic memory (and the applied dynamic space is not enough, and the space expansion operation is required)
int main() { int* p = (int*)malloc(5 * sizeof(int)); if (p == NULL) { perror("malloc"); return 1; } //use int i = 0; for (i = 0; i < 5; i++) { *(p + i) = 1; } //If it is not enough, add 5 integer spaces //Returns NULL if expansion fails int* ptr = (int*)realloc(p, 10 * sizeof(int)); if (ptr != NULL) { p = ptr; ptr = NULL; } //keep using space for (i = 0; i < 10; i++) { printf("%d ", *(p + i)); } free(p); p = NULL; return 0; }
The above code first opens up 5 integer spaces through the malloc function, and then continues to expand the space through the realloc function. When using the realloc function, pay attention to:
realloc On failure, return NULL. realloc When it fails, the original memory does not change, that is, it does not free or not move. If there is enough remaining memory behind the original memory, realloc memory=original memory+remaining memory,realloc Or return the address of the original memory; If there is not enough remaining memory behind the original memory, realloc Will apply for new memory, and then copy the original memory data to the new memory, the original memory will be free Lose,realloc Returns the address of the new memory. if size is 0, the effect is equivalent to free(). Pass to realloc The pointer must have been previously passed through malloc(), calloc() or realloc()distributed .