Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Toán tử new và delete trong C++


Toán tử new - Cấp phát bộ nhớ

Yêu cầu cấp phát bộ nhớ trên heap. Nếu đủ bộ nhớ, khởi tạo và trả về địa chỉ.

Cú pháp cơ bản:

pointer_variable = new datatype;

Khởi tạo giá trị:

pointer_variable = new datatype(value);

Cấp phát mảng:

pointer_variable = new datatype[size];

Ví dụ

#include <iostream>
using namespace std;

int main() {
    int* ptr1 = NULL;
    ptr1 = new int;
    float* ptr2 = new float(223.324);
    int* ptr3 = new int[28];
    *ptr1 = 28;
    cout << "Value of pointer variable 1: " << *ptr1 << endl;
    cout << "Value of pointer variable 2: " << *ptr2 << endl;
    if (!ptr3)
        cout << "Allocation of memory failed\n";
    else {
        for (int i = 10; i < 15; i++)
            ptr3[i] = i + 1;
        cout << "Value of store in block of memory: ";
        for (int i = 10; i < 15; i++)
            cout << ptr3[i] << " ";
    }
    return 0;
}

Kết quả

Value of pointer variable 1: 28
Value of pointer variable 2: 223.324
Value of store in block of memory: 11 12 13 14 15

Toán tử delete - Giải phóng bộ nhớ

Dùng để hủy cấp phát bộ nhớ. Người dùng tự giải phóng con trỏ đã tạo.

Cú pháp:

delete pointer_variable;

Xóa mảng:

delete[] pointer_variable;

Ví dụ

#include <iostream>
using namespace std;

int main() {
    int* ptr1 = NULL;
    ptr1 = new int;
    float* ptr2 = new float(299.121);
    int* ptr3 = new int[28];
    *ptr1 = 28;
    cout << "Value of pointer variable 1: " << *ptr1 << endl;
    cout << "Value of pointer variable 2: " << *ptr2 << endl;
    if (!ptr3)
        cout << "Allocation of memory failed\n";
    else {
        for (int i = 10; i < 15; i++)
            ptr3[i] = i + 1;
        cout << "Value of store in block of memory: ";
        for (int i = 10; i < 15; i++)
            cout << ptr3[i] << " ";
    }
    delete ptr1;
    delete ptr2;
    delete[] ptr3;
    return 0;
}

Kết quả

Value of pointer variable 1: 28
Value of pointer variable 2: 299.121
Value of store in block of memory: 11 12 13 14 15