logo

free()-funktio C-kirjastossa esimerkein

The free()-funktio C:ssä käytetään vapauttamaan tai jakamaan dynaamisesti varattu muisti ja se auttaa vähentämään muistin hukkaa. The C vapaa () -toimintoa ei voida käyttää staattisesti varatun muistin (esim. paikallisten muuttujien) tai pinoon varatun muistin vapauttamiseen. Sitä voidaan käyttää vain purkamaan keon muisti, joka on aiemmin varattu käyttämällä malloc(), calloc() ja realloc()-funktioita.

Free()-funktio on määritelty sisällä otsikkotiedosto.



free()-funktio C-kirjastossa

C free()-funktio

Free()-funktion syntaksi C:ssä

void free (void * ptr );>

Parametrit

    ptr on osoitin muistilohkoon, joka on vapautettava tai vapautettava.

Palautusarvo

  • Free()-funktio ei palauta mitään arvoa.

Esimerkkejä free()

Esimerkki 1:

Seuraava C-ohjelma havainnollistaa ohjelman käyttöä calloc() toiminto varaa muistia dynaamisesti ja vapaa() toiminto vapauttaa tuo muisti.

C




strint to int





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

>

>

Lähtö

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

Esimerkki 2:

Seuraava C-ohjelma havainnollistaa ohjelman käyttöä malloc() toiminto varaa muistia dynaamisesti ja vapaa() toiminto vapauttaa tuo muisti.

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

.on sama kuin java
>

>

Lähtö

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