C ++ brezplačno () - Standardna knjižnica C ++

Funkcija free () v jeziku C ++ sprosti blok pomnilnika, ki je bil predhodno dodeljen s funkcijami calloc, malloc ali realloc, zaradi česar je na voljo za nadaljnje dodeljevanje.

Funkcija free () v jeziku C ++ sprosti blok pomnilnika, ki je bil predhodno dodeljen s funkcijami calloc, malloc ali realloc, zaradi česar je na voljo za nadaljnje dodeljevanje.

Funkcija free () ne spremeni vrednosti kazalca, to pomeni, da še vedno kaže na isto mesto v pomnilniku.

brezplačni () prototip

 brez praznine (void * ptr);

Funkcija je definirana v glavi datoteke.

free () Parametri

  • ptr: Kazalec na pomnilniški blok, ki je bil prej dodeljen z malloc, calloc ali realloc. Kazalec je lahko ničen ali pa ne kaže na blok pomnilnika, ki ga dodelijo funkcije calloc, malloc ali realloc.
    • Če je ptr nič, funkcija free () ne naredi ničesar.
    • Če ptr ne kaže na pomnilniški blok, ki ga dodelijo funkcije calloc, malloc ali realloc, povzroči nedefinirano vedenje.

free () Vrnjena vrednost

Funkcija free () ne vrne ničesar. Preprosto nam da na voljo pomnilniški blok.

Primer 1: Kako funkcija free () deluje z 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; )

Ko zaženete program, bo rezultat:

 Vnesite 5 celih števil 21 3 -10 -13 45 Uporabnik vnese vrednost 21 3 -10 -13 45 Vrednost smeti 6690624 0 6685008 0 45

Primer 2: Kako funkcija free () deluje s 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; )

Ko zaženete program, bo rezultat:

 Pred sprostitvijo Naslov = 0x6a1530 Vrednost = 5.233 Po sprostitvi Naslov = 0x6a1530 Vrednost = 9.7429e-039

Primer 3: Kako funkcija free () deluje z 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

Zanimive Članki...