C ++ funkcija free () paskirsto atminties bloką, anksčiau skirtą naudojant calloc, malloc ar realloc funkcijas, suteikiant galimybę jį toliau paskirstyti.
C ++ funkcija free () paskirsto atminties bloką, anksčiau skirtą naudojant calloc, malloc ar realloc funkcijas, suteikiant galimybę jį toliau paskirstyti.
Funkcija free () nekeičia rodyklės vertės, ty ji vis tiek nurodo tą pačią atminties vietą.
free () prototipas
tuštumas laisvas (void * ptr);
Funkcija apibrėžta antraštės faile.
nemokamai () parametrai
- ptr: žymeklis į atminties bloką, anksčiau priskirtą malloc, calloc ar realloc. Rodyklė gali būti lygi nuliui arba nenurodyti atminties bloko, skirto calloc, malloc ar realloc funkcijomis.
- Jei ptr yra nulinė, funkcija free () nieko nedaro.
- Jei ptr nenurodo atminties bloko, skirto calloc, malloc ar realloc funkcijomis, tai sukelia neapibrėžtą elgesį.
free () Grąžinimo vertė
Funkcija free () nieko negrąžina. Tai tiesiog leidžia mums naudotis atminties bloku.
1 pavyzdys: Kaip funkcija free () veikia su 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; )
Kai paleisite programą, išvestis bus:
Įveskite 5 sveikus skaičius 21 3 -10 -13 45 Vartotojo įvesta vertė 21 3 -10 -13 45 Šiukšlių vertė 6690624 0 6685008 0 45
2 pavyzdys: Kaip funkcija free () veikia su 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; )
Kai paleisite programą, išvestis bus:
Prieš atlaisvinant adresą = 0x6a1530 Reikšmė = 5.233 Atlaisvinus adresą = 0x6a1530 Reikšmė = 9.7429e-039
3 pavyzdys: Kaip funkcija free () veikia su 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