當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ free()用法及代碼示例

C++ 中的free() 函數釋放先前使用 calloc、malloc 或 realloc 函數分配的內存塊,使其可用於進一步分配。

C++ 中的free() 函數釋放先前使用 calloc、malloc 或 realloc 函數分配的內存塊,使其可用於進一步分配。

free() 函數不會改變指針的值,即它仍然指向同一個內存位置。

free()原型

void free(void *ptr);

該函數在<cstdlib> 頭文件中定義。

參數:

  • ptr: 指向先前使用 malloc、calloc 或 realloc 分配的內存塊的指針。指針可能為空,也可能不指向由 calloc、malloc 或 realloc 函數分配的內存塊。
    • 如果 ptr 為空,free() 函數什麽也不做。
    • 如果 ptr 不指向由 calloc、malloc 或 realloc 函數分配的內存塊,則會導致未定義的行為。

返回:

free() 函數不返回任何內容。它隻是使內存塊可供我們使用。

示例 1:free() 函數如何與 malloc() 一起使用?

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int *ptr;
    ptr = (int*) malloc(5*sizeof(int));
    cout << "Enter 5 integers" << endl;

    for (int i=0; i<5; i++)
    {
    // *(ptr+i) can be replaced by ptr[i]
        cin >> *(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;
}

運行程序時,輸出將是:

Enter 5 integers
21 3 -10 -13 45
User entered value
21 3 -10 -13 45
Garbage Value
6690624 0 6685008 0 45

示例 2:free() 函數如何與 calloc() 一起使用?

#include <iostream>
#include <cstdlib>
#include <cstring>
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;
}

運行程序時,輸出將是:

Before freeing
Address = 0x6a1530
Value = 5.233
After freeing
Address = 0x6a1530
Value = 9.7429e-039

示例 3:free() 函數如何與 realloc() 一起使用?

#include <iostream>
#include <cstdlib>
#include <cstring>
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: " <<ptr << endl;

    free(ptr);
    /* prints a garbage value after ptr is free */
    cout << endl << "Garbage Value: " << ptr;

    return 0;
}

運行程序時,輸出將是:

Before reallocating: Hello C++
After reallocating: Hello, Welcome to C++
Garbage Value: @↨/

示例 4:free() 函數與其他情況

#include <iostream>
#include <cstdlib>
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;
}

運行程序時,輸出將是:

Pointer is Null
5

相關用法


注:本文由純淨天空篩選整理自 C++ free()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。