The функція free() у C використовується для звільнення або звільнення динамічно виділеної пам’яті та допомагає зменшити втрату пам’яті. The C безкоштовно() не можна використовувати для звільнення статично виділеної пам’яті (наприклад, локальних змінних) або пам’яті, виділеної в стеку. Його можна використовувати лише для звільнення пам’яті купи, раніше виділеної за допомогою функцій malloc(), calloc() і realloc().
Функція free() визначена всередині файл заголовка.

Функція C free().
Синтаксис функції free() у C
void free (void * ptr );>
Параметри
- ptr — вказівник на блок пам’яті, який потрібно звільнити або звільнити.
Повернене значення
- Функція free() не повертає жодного значення.
Приклади free()
приклад 1:
Наступна програма на C ілюструє використання calloc() функція динамічного розподілу пам'яті та безкоштовно() функцію для звільнення цієї пам’яті.
C
підключення до бази даних у java
// 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;> }> |
>
>Вихід
Enter number of Elements: 5 Successfully allocated the memory using calloc(). Calloc Memory Successfully freed.>
приклад 2:
Наступна програма на C ілюструє використання malloc() функція динамічного розподілу пам'яті та безкоштовно() функцію для звільнення цієї пам’яті.
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;> }> |
>
>Вихід
Enter number of Elements: 5 Successfully allocated the memory using malloc(). Malloc Memory Successfully freed.>