logo

free() Funkcija C bibliotekoje su pavyzdžiais

The free() funkcija C naudojamas dinamiškai paskirstytai atminčiai atlaisvinti arba paskirstyti ir padeda sumažinti atminties švaistymą. The C nemokama () funkcija negali būti naudojama statiškai paskirstytai atminčiai (pvz., vietiniams kintamiesiems) arba kamino atminčiai išlaisvinti. Jis gali būti naudojamas tik krūvos atminčiai, anksčiau paskirtai naudojant malloc(), calloc() ir realloc() funkcijas, panaikinti.

Funkcija free () yra apibrėžta viduje antraštės failą.



free() funkcija C bibliotekoje

C free() funkcija

Funkcijos free () sintaksė C

void free (void * ptr );>

Parametrai

    ptr yra rodyklė į atminties bloką, kurį reikia atlaisvinti arba panaikinti.

Grąžinimo vertė

  • Funkcija free () negrąžina jokios reikšmės.

Free() pavyzdžiai

1 pavyzdys:

Toliau pateikta C programa iliustruoja, kaip naudoti calloc() funkcija dinamiškai paskirstyti atmintį ir Laisvas() funkcija atlaisvinti tą atmintį.

C




pasirinkite sql iš kelių lentelių





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

>

>

Išvestis

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

2 pavyzdys:

Toliau pateikta C programa iliustruoja, kaip naudoti malloc () funkcija dinamiškai paskirstyti atmintį ir Laisvas() funkcija atlaisvinti tą atmintį.

C




apvalkalo scenarijų padarymas vykdomu

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

>

>

Išvestis

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