Fungsi free () di C ++ membatalkan alokasi blok memori yang sebelumnya dialokasikan menggunakan fungsi calloc, malloc atau realloc, membuatnya tersedia untuk alokasi lebih lanjut.
Fungsi free () di C ++ membatalkan alokasi blok memori yang sebelumnya dialokasikan menggunakan fungsi calloc, malloc atau realloc, membuatnya tersedia untuk alokasi lebih lanjut.
Fungsi free () tidak mengubah nilai penunjuk, yaitu masih menunjuk ke lokasi memori yang sama.
free () prototipe
batal gratis (batal * ptr);
Fungsi tersebut didefinisikan dalam file header.
free () Parameter
- ptr: Penunjuk ke blok memori yang sebelumnya dialokasikan dengan malloc, calloc atau realloc. Pointer mungkin nol atau mungkin tidak menunjuk ke blok memori yang dialokasikan oleh fungsi calloc, malloc atau realloc.
- Jika ptr nol, fungsi free () tidak melakukan apa-apa.
- Jika ptr tidak menunjuk ke blok memori yang dialokasikan oleh fungsi calloc, malloc atau realloc, itu menyebabkan perilaku tidak terdefinisi.
bebas () Nilai kembali
Fungsi free () tidak mengembalikan apa pun. Itu hanya membuat blok memori tersedia untuk kita.
Contoh 1: Bagaimana fungsi free () bekerja dengan 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; )
Saat Anda menjalankan program, hasilnya adalah:
Masukkan 5 bilangan bulat 21 3 -10 -13 45 Pengguna memasukkan nilai 21 3 -10 -13 45 Nilai Sampah 6690624 0 6685008 0 45
Contoh 2: Bagaimana fungsi free () bekerja dengan 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; )
Saat Anda menjalankan program, hasilnya adalah:
Sebelum membebaskan Alamat = 0x6a1530 Nilai = 5.233 Setelah membebaskan Alamat = 0x6a1530 Nilai = 9.7429e-039
Contoh 3: Bagaimana fungsi free () bekerja dengan 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