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.

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

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile