C ++ bezmaksas () - C ++ standarta bibliotēka

C ++ funkcija free () atdala atmiņas bloku, kas iepriekš piešķirts, izmantojot calloc, malloc vai realloc funkcijas, padarot to pieejamu turpmākiem piešķīrumiem.

C ++ funkcija free () atdala atmiņas bloku, kas iepriekš piešķirts, izmantojot calloc, malloc vai realloc funkcijas, padarot to pieejamu turpmākiem piešķīrumiem.

Funkcija free () nemaina rādītāja vērtību, tas ir, tā joprojām norāda uz to pašu atmiņas vietu.

bezmaksas () prototips

 void free (void * ptr);

Funkcija ir definēta galvenes failā.

bezmaksas () parametri

  • ptr: rādītājs uz atmiņas bloku, kas iepriekš piešķirts ar malloc, calloc vai realloc. Rādītājs var būt nulle vai nenorādīt uz atmiņas bloku, ko piešķir calloc, malloc vai realloc funkcijas.
    • Ja ptr ir nulle, funkcija free () neko nedara.
    • Ja ptr nenorāda uz atmiņas bloku, ko piešķir calloc, malloc vai realloc funkcijas, tas izraisa nedefinētu uzvedību.

free () Atgriešanās vērtība

Funkcija free () neko neatgriež. Tas vienkārši padara atmiņas bloku pieejamu mums.

1. piemērs: Kā funkcija free () darbojas ar 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; )

Palaidot programmu, izeja būs:

 Ievadiet 5 veselus skaitļus 21 3 -10 -13 45 Lietotāja ievadītā vērtība 21 3 -10 -13 45 Atkritumu vērtība 6690624 0 6685008 0 45

2. piemērs: Kā funkcija free () darbojas ar 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; )

Palaidot programmu, izeja būs:

 Pirms adreses atbrīvošanas = 0x6a1530 vērtība = 5,233 Pēc adreses atbrīvošanas = 0x6a1530 vērtība = 9,7429e-039

3. piemērs: Kā funkcija free () darbojas ar 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

Interesanti raksti...