Memory management in C
This chapter explains how to manage dynamic memory in C language. Programming language C provides several different functions for allocating and managing memory. These functions can be found in the Header file.
void * calloc (int tongkichco, int kichco);
This function allocates an array of elements whose total size is tongkichco whose size of each element calculated in bytes will be kichco.
void free (void * diachi);
This function frees a memory block defined by diachi.
void * malloc (int tongkichco);
This function allocates dynamic memory with tongkichco size.
void * realloc (void * diachi, int kichco_moi);
This function changes the size of allocated memory to a new size kichco_moi.
Allocate dynamic memory in C
When you program, you must be aware of the size of an array, then it is easy to define the array. For example, if you store a name of any person, it can be up to 100 characters so you can define the following:
char ten_mang [100];
Now consider the case where you don't have an idea of the size of the array you intend to store, for example, you want to store a detailed description of a topic. Here you need to define a pointer to the character that does not define how much memory is required and then rely on the request we will allocate memory as the example below:
#include #include #include int main () { char tennhanvien [ 100 ]; char * mieuta ; strcpy ( tennhanvien , "Tran Minh Chinh" ); /* Cap phat bo nho dong */ mieuta = ( char *) malloc ( 200 ); if ( mieuta == NULL ) { fprintf ( stderr , "Error - khong the cap phat bo nho theo yeu caun" ); } else { strcpy ( mieuta , "Chinh la nhan vien IT co nang luc chem gio tot!!!" ); } printf ( "Ten nhan vien la: %sn" , tennhanvien ); printf ( "Mieu ta: %sn" , mieuta ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); }
Compile and run the above C program to see the results:
The above program can be written using calloc (), instead of malloc as follows:
mieuta = ( char *) calloc ( 200 , sizeof ( char ));
So you have complete control of memory allocation and you can transmit any size value while allocating memory, unlike a fixed-length array that cannot be changed.
Change and release memory in C
When your program finishes, the operating system will automatically release memory allocated to the program, but in fact when you don't need memory anymore, you should free up memory by using the free () function . .
Alternatively, you can increase or decrease the size of the allocated memory block by calling realloc () . Please check the above program once and use the realloc () and free () functions :
#include #include #include int main () { char tennhanvien [ 100 ]; char * mieuta ; strcpy ( tennhanvien , "Tran Minh Chinh" ); /* Cap phat bo nho dong */ mieuta = ( char *) malloc ( 100 ); if ( mieuta == NULL ) { fprintf ( stderr , "Error - khong the cap phat bo nho theo yeu caun" ); } else { strcpy ( mieuta , "Chinh la nhan vien IT co nang luc chem gio tot!!!" ); } /* Gia su ban muon luu tru mot mieuta nho hon */ mieuta = ( char *) calloc ( 50 , sizeof ( char )); if ( mieuta == NULL ) { fprintf ( stderr , "Error - khong the cap phat bo nho theo yeu caun" ); } else { strcat ( mieuta , "Anh ta rat gioi!!!" ); } printf ( "Ten nhan vien: %sn" , tennhanvien ); printf ( "Mieu ta: %sn" , mieuta ); /* giai phong bo nho voi ham free() */ free ( mieuta ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); }
Compile and run the above C program to see the results:
You can try the above example without using additional allocations and strcat () will report an error due to insufficient memory allocation.
According to Tutorialspoint
Previous article: Variable parameter in C
Next lesson: Command line parameter in C
You should read it
- How does ROM memory work?
- How to fix BSOD Memory Management error
- Dynamic memory in C ++
- The US government urges coders to use 'memory-safe programming languages'
- Variable in C programming
- Microsoft releases tool to help detect memory leaks with Edge
- Useful tips to free iPhone memory
- How to determine if computer memory has a problem?
- 'Remember your face and forget your name', remedy and method for practicing super memory
- Samsung will launch a 1TB memory chip for smartphones, will it appear on Galaxy S10?
- How to test iPhone 6/6 Plus using MLC or TLC memory
- Android phone full of memory, what to do to fix?