Funkcia free() v knižnici C s príkladmi

Funkcia free() v knižnici C s príkladmi

The funkcia free() v C sa používa na uvoľnenie alebo uvoľnenie dynamicky pridelenej pamäte a pomáha znižovať plytvanie pamäťou. The C free() Funkciu nemožno použiť na uvoľnenie staticky alokovanej pamäte (napr. lokálne premenné) alebo pamäte alokovanej v zásobníku. Dá sa použiť len na uvoľnenie pamäte haldy, ktorá bola predtým pridelená pomocou funkcií malloc(), calloc() a realloc().

Vo vnútri je definovaná funkcia free(). hlavičkový súbor.

funkcia free() v knižnici C

Funkcia C free().

Syntax funkcie free() v jazyku C

void free (void * ptr ); 

Parametre

    ptr je ukazovateľ na blok pamäte, ktorý je potrebné uvoľniť alebo uvoľniť.

Návratová hodnota

  • Funkcia free() nevracia žiadnu hodnotu.

Príklady free()

Príklad 1:

Nasledujúci program C ilustruje použitie calloc() funkcia na dynamické prideľovanie pamäte a zadarmo() funkciu na uvoľnenie tejto pamäte.

C




// C program to demonstrate use of> // free() function using calloc()> #include> #include> int> main()> {> > // This pointer ptr will hold the> > // base address of the block created> > int> * ptr;> > int> n = 5;> > // Get the number of elements for the array> > printf> (> 'Enter number of Elements: %d '> , n);> > scanf> (> '%d'> , &n);> > // Dynamically allocate memory using calloc()> > ptr = (> int> *)> calloc> (n,> sizeof> (> int> ));> > // Check if the memory has been successfully> > // allocated by calloc() or not> > if> (ptr == NULL) {> > printf> (> 'Memory not allocated '> );> > exit> (0);> > }> > // Memory has been Successfully allocated using calloc()> > printf> (> 'Successfully allocated the memory using '> > 'calloc(). '> );> > // Free the memory> > free> (ptr);> > printf> (> 'Calloc Memory Successfully freed.'> );> > return> 0;> }>

Výkon

Enter number of Elements: 5 Successfully allocated the memory using calloc(). Calloc Memory Successfully freed. 

Príklad 2:

Nasledujúci program C ilustruje použitie malloc() funkcia na dynamické prideľovanie pamäte a zadarmo() funkciu na uvoľnenie tejto pamäte.

C




// C program to demonstrate use of> // free() function using malloc()> #include> #include> int> main()> {> > // This pointer ptr will hold the> > // base address of the block created> > int> * ptr;> > int> n = 5;> > // Get the number of elements for the array> > printf> (> 'Enter number of Elements: %d '> , n);> > scanf> (> '%d'> , &n);> > // Dynamically allocate memory using malloc()> > ptr = (> int> *)> malloc> (n *> sizeof> (> int> ));> > // Check if the memory has been successfully> > // allocated by malloc() or not> > if> (ptr == NULL) {> > printf> (> 'Memory not allocated '> );> > exit> (0);> > }> > // Memory has been Successfully allocated using malloc()> > printf> (> 'Successfully allocated the memory using '> > 'malloc(). '> );> > // Free the memory> > free> (ptr);> > printf> (> 'Malloc Memory Successfully freed.'> );> > return> 0;> }>

Výkon

Enter number of Elements: 5 Successfully allocated the memory using malloc(). Malloc Memory Successfully freed.