logo

free() Funktion i C-bibliotek med exempel

De free() funktion i C används för att frigöra eller omallokera det dynamiskt allokerade minnet och hjälper till att minska minnesslöseri. De C gratis() funktionen kan inte användas för att frigöra det statiskt allokerade minnet (t.ex. lokala variabler) eller minne som allokerats på stacken. Den kan endast användas för att deallokera heapminnet som tidigare allokerats med funktionerna malloc(), calloc() och realloc().

Funktionen free() är definierad inuti header-fil.



free()-funktionen i C-biblioteket

C free() Funktion

Syntax för free() Funktion i C

void free (void * ptr );>

Parametrar

    ptr är pekaren till minnesblocket som behöver frigöras eller avallokeras.

Returvärde

  • Funktionen free() returnerar inget värde.

Exempel på gratis()

Exempel 1:

Följande C-program illustrerar användningen av calloc() funktion för att tilldela minne dynamiskt och fri() funktion för att frigöra minnet.

C




hur man blockerar YouTube-annonser på Android





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

>

>

Produktion

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

Exempel 2:

Följande C-program illustrerar användningen av malloc() funktion för att tilldela minne dynamiskt och fri() funktion för att frigöra minnet.

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

sort array lista java
>

>

Produktion

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