C ++ free () - C ++ Standardbibliothek

Die Funktion free () in C ++ gibt die Zuweisung eines zuvor mithilfe von Calloc-, Malloc- oder Realloc-Funktionen zugewiesenen Speicherblocks frei und stellt ihn für weitere Zuweisungen zur Verfügung.

Die Funktion free () in C ++ gibt die Zuweisung eines zuvor mithilfe von Calloc-, Malloc- oder Realloc-Funktionen zugewiesenen Speicherblocks frei und stellt ihn für weitere Zuweisungen zur Verfügung.

Die Funktion free () ändert den Wert des Zeigers nicht, dh sie zeigt immer noch auf denselben Speicherort.

free () Prototyp

 nichtig frei (nichtig * ptr);

Die Funktion ist in der Header-Datei definiert.

free () Parameter

  • ptr: Ein Zeiger auf einen Speicherblock, der zuvor malloc, calloc oder realloc zugewiesen wurde. Der Zeiger kann null sein oder nicht auf einen Speicherblock zeigen, der von Calloc-, Malloc- oder Realloc-Funktionen zugewiesen wird.
    • Wenn ptr null ist, führt die Funktion free () nichts aus.
    • Wenn ptr nicht auf einen Speicherblock verweist, der von Calloc-, Malloc- oder Realloc-Funktionen zugewiesen wurde, führt dies zu undefiniertem Verhalten.

free () Rückgabewert

Die Funktion free () gibt nichts zurück. Es stellt uns einfach den Speicherblock zur Verfügung.

Beispiel 1: Wie funktioniert die Funktion free () mit 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; )

Wenn Sie das Programm ausführen, lautet die Ausgabe wie folgt:

 Geben Sie 5 Ganzzahlen ein. 21 3 -10 -13 45 Vom Benutzer eingegebener Wert 21 3 -10 -13 45 Müllwert 6690624 0 6685008 0 45

Beispiel 2: Wie funktioniert die Funktion free () mit 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; )

Wenn Sie das Programm ausführen, lautet die Ausgabe wie folgt:

 Vor dem Freigeben von Adresse = 0x6a1530 Wert = 5.233 Nach dem Freigeben von Adresse = 0x6a1530 Wert = 9.7429e-039

Beispiel 3: Wie funktioniert die Funktion free () mit 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

Interessante Beiträge...