Funció free() a la biblioteca C amb exemples
El funció free() en C s'utilitza per alliberar o desassignar la memòria assignada dinàmicament i ajuda a reduir el malbaratament de memòria. El C lliure() La funció no es pot utilitzar per alliberar la memòria assignada estàticament (per exemple, variables locals) o la memòria assignada a la pila. Només es pot utilitzar per desassignar la memòria de pila prèviament assignada mitjançant les funcions malloc(), calloc() i realloc().
La funció free() es defineix dins fitxer de capçalera.
Funció C free().
Sintaxi de la funció free() en C
void free (void * ptr );
Paràmetres
- ptr és el punter al bloc de memòria que cal alliberar o desassignar.
Valor de retorn
- La funció free() no retorna cap valor.
Exemples de free()
Exemple 1:
El programa C següent il·lustra l'ús de la calloc() funció per assignar memòria dinàmicament i gratuït () funció per alliberar aquesta memòria.
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;> }> |
Sortida
Enter number of Elements: 5 Successfully allocated the memory using calloc(). Calloc Memory Successfully freed.
Exemple 2:
El següent programa en C il·lustra l'ús del malloc() funció per assignar memòria dinàmicament i gratuït () funció per alliberar aquesta memòria.
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;> }> |
Sortida
Enter number of Elements: 5 Successfully allocated the memory using malloc(). Malloc Memory Successfully freed.