Функція free () у C ++ звільняє блок пам'яті, раніше виділений за допомогою функцій calloc, malloc або realloc, роблячи його доступним для подальшого розподілу.
Функція free () у C ++ звільняє блок пам'яті, раніше виділений за допомогою функцій calloc, malloc або realloc, роблячи його доступним для подальшого розподілу.
Функція free () не змінює значення покажчика, тобто вона все одно вказує на те саме місце пам'яті.
безкоштовний () прототип
порожнеча вільна (void * ptr);
Функція визначена у файлі заголовка.
free () Параметри
- ptr: вказівник на блок пам'яті, раніше виділений malloc, calloc або realloc. Вказівник може бути нульовим або не вказувати на блок пам'яті, виділений функціями calloc, malloc або realloc.
- Якщо ptr має значення null, функція free () нічого не робить.
- Якщо ptr не вказує на блок пам'яті, виділений функціями calloc, malloc або realloc, це спричиняє невизначену поведінку.
free () Повернене значення
Функція free () нічого не повертає. Це просто робить блок пам'яті доступним для нас.
Приклад 1: Як функція free () працює з malloc ()?
#include #include using namespace std; int main() ( int *ptr; ptr = (int*) malloc(5*sizeof(int)); cout << "Enter 5 integers" << endl; for (int i=0; i> *(ptr+i); ) cout << endl << "User entered value"<< endl; for (int i=0; i<5; i++) ( cout << *(ptr+i) << " "; ) free(ptr); /* prints a garbage value after ptr is free */ cout << "Garbage Value" << endl; for (int i=0; i<5; i++) ( cout << *(ptr+i) << " "; ) return 0; )
Коли ви запускаєте програму, результат буде:
Введіть 5 цілих чисел 21 3 -10 -13 45 Введене користувачем значення 21 3 -10 -13 45 Значення сміття 6690624 0 6685008 0 45
Приклад 2: Як функція free () працює з calloc ()?
#include #include #include using namespace std; int main() ( float *ptr; ptr = (float*) calloc(1,sizeof(float)); *ptr = 5.233; cout << "Before freeing" << endl; cout << "Address = " << ptr << endl; cout << "Value = " << *ptr << endl; free(ptr); cout << "After freeing" << endl; /* ptr remains same, *ptr changes*/ cout << "Address = " << ptr << endl; cout << "Value = " << *ptr << endl; return 0; )
Коли ви запускаєте програму, результат буде:
До звільнення Адреса = 0x6a1530 Значення = 5.233 Після звільнення Адреса = 0x6a1530 Значення = 9.7429e-039
Приклад 3: Як функція free () працює з realloc ()?
#include #include #include using namespace std; int main() ( char *ptr; ptr = (char*) malloc(10*sizeof(char)); strcpy(ptr,"Hello C++"); cout << "Before reallocating: " << ptr << endl; /* reallocating memory */ ptr = (char*) realloc(ptr,20); strcpy(ptr,"Hello, Welcome to C++"); cout << "After reallocating: " <
When you run the program, the output will be:
Before reallocating: Hello C++ After reallocating: Hello, Welcome to C++ Garbage Value: @↨/
Example 4: free() function with other cases
#include #include using namespace std; int main() ( int x = 5; int *ptr1 = NULL; /* allocatingmemory without using calloc, malloc or realloc*/ int *ptr2 = &x; if(ptr1) ( cout << "Pointer is not Null" << endl; ) else ( cout << "Pointer is Null" << endl; ) /* Does nothing */ free(ptr1); cout << *ptr2; /* gives a runtime error if free(ptr2) is executed*/ // free(ptr2); return 0; )
When you run the program, the output will be:
Pointer is Null 5