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
- 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?
May be interested
- Android phone full of memory, what to do to fix?android phones are full of memory, a phenomenon that often occurs with smartphones with low internal memory, or users who store too many applications on their mobile devices. if you do not want this situation to occur, apply 4 small tips below.
- How to turn the memory card into internal memory on Android 6.0turning the memory card into internal memory on android 6.0 will give you more space to store data and increase device memory.
- How to handle a computer error message Your computer is low on memory on Windowsyou are using the computer, the screen suddenly appears your computer is low on memory error message, along with the applications that you are opening will be closed. even if you reboot the machine, it still can't be fixed, because this is a memory error.
- How to fix memory leaks on Androidif your android phone is constantly having problems, you can identify it from three potential causes: malware infection, battery drain or memory leak. this article will guide you how to troubleshoot memory leaks on android.
- 7 Ways to increase memory and ability to concentrate in learningwith 7 ways to enhance your learning memory below, you will be able to be more confident on the way to conquer knowledge to bring the highest results.
- How to manage memory to restrict Linux to use too much RAMyou install linux with the promise that it will consume less system resources than windows. but why then, is your system still slow?
- How to Format a Memory Card Using a Windows Computertipsmake today will teach you how to erase and reformat the memory card. memory cards are commonly used as memory on cameras and tablets; to use a memory card on a specific operating system, you need to format it first.
- Microsoft releases tool to help detect memory leaks with Edgememory leak is a common problem in programming, when a piece of code is not managed and allocated memory properly after it stops running.
- Buy genuine memory cards, cheap where Hanoi & Ho Chi Minh City?unlike the appearance, the memory card is an important tool of the technology people with perfect storage capacity. however, how to buy a good memory card?
- How to free up to 6GB of internal memory for iPhoneapplying a small trick in this article, you can free up to nearly 6gb of internal memory for your iphone. this number for 16gb iphone users that is indeed a dream thing.