logo

Funkcija free() v knjižnici C s primeri

The funkcija free() v C se uporablja za sprostitev ali sprostitev dinamično dodeljenega pomnilnika in pomaga pri zmanjševanju izgube pomnilnika. The C brezplačno () funkcije ni mogoče uporabiti za sprostitev statično dodeljenega pomnilnika (npr. lokalnih spremenljivk) ali pomnilnika, dodeljenega na skladu. Uporablja se lahko samo za sprostitev pomnilnika kopice, ki je bil predhodno dodeljen s funkcijami malloc(), calloc() in realloc().

Funkcija free() je definirana znotraj naslovna datoteka.



funkcija free() v knjižnici C

Funkcija C free().

Sintaksa funkcije free() v C

void free (void * ptr );>

Parametri

    ptr je kazalec na pomnilniški blok, ki ga je treba sprostiti ali sprostiti.

Povratna vrednost

  • Funkcija free() ne vrne nobene vrednosti.

Primeri free()

Primer 1:

Naslednji program C ponazarja uporabo calloc() funkcijo za dinamično dodeljevanje pomnilnika in prost() funkcijo za sprostitev tega spomina.

C




vse velike črke ukaz excel





// 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;> }>

>

>

Izhod

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

Primer 2:

Naslednji program C ponazarja uporabo malloc() funkcijo za dinamično dodeljevanje pomnilnika in prost() funkcijo za sprostitev tega spomina.

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;> }>

c# vadnica

>

>

Izhod

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