Table of Contents
This guide covers memory management in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
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.
How to Change and Release Memory in C
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
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.