C ++ 'daki free () işlevi, calloc, malloc veya realloc işlevleri kullanılarak önceden tahsis edilmiş bir bellek bloğunu serbest bırakarak onu daha fazla tahsis için kullanılabilir hale getirir.
C ++ 'daki free () işlevi, calloc, malloc veya realloc işlevleri kullanılarak önceden tahsis edilmiş bir bellek bloğunu serbest bırakarak onu daha fazla tahsis için kullanılabilir hale getirir.
Free () işlevi, işaretçinin değerini değiştirmez, yani yine de aynı bellek konumuna işaret eder.
ücretsiz () prototip
boşluksuz (void * ptr);
İşlev, başlık dosyasında tanımlanır.
free () Parametreler
- ptr: Daha önce malloc, calloc veya realloc ile ayrılmış bir bellek bloğuna işaretçi. Gösterici boş olabilir veya calloc, malloc veya realloc fonksiyonları tarafından ayrılan bir bellek bloğunu göstermeyebilir.
- Ptr null ise, free () işlevi hiçbir şey yapmaz.
- Ptr calloc, malloc veya realloc fonksiyonları tarafından ayrılan bir bellek bloğuna işaret etmezse, tanımsız davranışa neden olur.
free () Dönüş değeri
Free () işlevi hiçbir şey döndürmez. Hafıza bloğunu bizim için kullanılabilir hale getirir.
Örnek 1: free () işlevi malloc () ile nasıl çalışır?
#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; )
Programı çalıştırdığınızda, çıktı:
5 tam sayı girin 21 3-10 -13 45 Kullanıcı girilen değer 21 3-10 -13 45 Çöp Değeri 6690624 0 6685008 0 45
Örnek 2: free () işlevi calloc () ile nasıl çalışır?
#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; )
Programı çalıştırdığınızda, çıktı:
Adresi serbest bırakmadan önce = 0x6a1530 Değer = 5.233 Adresi serbest bıraktıktan sonra = 0x6a1530 Değer = 9.7429e-039
Örnek 3: free () işlevi realloc () ile nasıl çalışır?
#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