C memory management
This chapter will explain dynamic memory management in C. The C language provides several functions for memory allocation and management. These functions can be found in the <stdlib.h> header file.
Serial number | Function and description |
---|---|
1 | void *calloc (int num, int size); This function allocates an array with num elements, each element is size bytes in size. |
2 | void free(void *address); This function releases the h memory block pointed to by address. |
3 | void *malloc(int num); This function allocates a num bytes arrays and initialize them. |
4 | ##void *realloc(void *address, int newsize); This function reallocates memory and expands memory newsize. |
Dynamic allocation of memoryWhen programming, it is easier to define the array if you know the size of the array in advance. For example, an array that stores people's names can hold up to 100 characters, so you can define the array as follows:
char name[100];However, if you don't know in advance the length of text that needs to be stored, for example, you want to store the relevant A detailed description of a topic. Here, we need to define a pointer that points to a character with undefined memory size, and then allocate memory according to needs, as shown below:
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){ char name[100]; char *description; strcpy(name, "Zara Ali"); /* 动态分配内存 */ description = malloc( 200 * sizeof(char) ); if( description == NULL ) { fprintf(stderr, "Error - unable to allocate required memory\n"); } else { strcpy( description, "Zara ali a DPS student in class 10th"); } printf("Name = %s\n", name ); printf("Description: %s\n", description );}When the above code is compiled and executed, It will produce the following results:
Name = Zara AliDescription: Zara ali a DPS student in class 10thThe above program can also be written using
calloc(), just replace malloc with calloc, as shown below:
calloc(200, sizeof(char));When allocating memory dynamically, you have full control and can pass values of any size. Arrays with pre-defined sizes cannot be changed in size once defined. Resize the memory and release memoryWhen the program exits, the operating system will automatically release all the memory allocated to the program. However, it is recommended that you call it whenever you do not need the memory. Function
free() to release memory.
Alternatively, you can increase or decrease the size of the allocated memory block by calling the functionrealloc(). Let's look at the above example again using the realloc() and free() functions:
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){ char name[100]; char *description; strcpy(name, "Zara Ali"); /* 动态分配内存 */ description = malloc( 30 * sizeof(char) ); if( description == NULL ) { fprintf(stderr, "Error - unable to allocate required memory\n"); } else { strcpy( description, "Zara ali a DPS student."); } /* 假设您想要存储更大的描述信息 */ description = realloc( description, 100 * sizeof(char) ); if( description == NULL ) { fprintf(stderr, "Error - unable to allocate required memory\n"); } else { strcat( description, "She is in class 10th"); } printf("Name = %s\n", name ); printf("Description: %s\n", description ); /* 使用 free() 函数释放内存 */ free(description);}When the above code is compiled and executed, it produces the following results:
Name = Zara AliDescription: Zara ali a DPS student.She is in class 10thYou can If you try without reallocating additional memory, the strcat() function will generate an error because there is insufficient memory available to store the description.