63. Unique Pointer Custom Deleter

#include <iostream>
#include <cstdint>
#include <memory>
using namespace std;

class DataBlock {
public:
    uint8_t* buf1;
    uint8_t* buf2;
    int size;

    DataBlock(int n) : size(n) {
        buf1 = new uint8_t[n];
        buf2 = new uint8_t[n];
    }

    void set(int index, uint8_t value) {
        buf1[index] = value;
        buf2[index] = value;
    }

    void print() const {
        for (int i = 0; i < size; i++) {
            cout << (int)buf1[i];
            if (i != size - 1) cout << " ";
        }
        cout << endl;
    }
};

DataBlock* createDataBlock(int n) {
    return new DataBlock(n);
}

void cleanup(DataBlock* p) {
    delete[] p->buf1;
    delete[] p->buf2;
    delete p;
    cout << "Object cleaned" << endl;
}

int main() {
    int N;
    cin >> N;

    {
        unique_ptr<DataBlock, decltype(&cleanup)> obj(
            createDataBlock(N), cleanup
        );

        for (int i = 0; i < N; i++) {
            int temp;
            cin >> temp;
            obj->set(i, static_cast<uint8_t>(temp));
        }

        obj->print();
    }

    return 0;
}

Explanation & Logic Summary:
The object is dynamically created inside a factory function and owns multiple internal buffers. Because proper destruction requires releasing several resources in a defined order, a standalone cleanup function is required. By attaching this function as a custom deleter to std::unique_ptr, ownership transfer and deterministic cleanup are guaranteed. When the unique_ptr goes out of scope, the cleanup function is invoked automatically and exactly once.

Firmware Relevance & Real-World Context:
In firmware and systems software, objects frequently encapsulate multiple low-level resources such as buffers, hardware descriptors, or handles that must be released in a strict sequence. Using std::unique_ptr with a custom deleter provides deterministic cleanup, prevents memory leaks, and aligns with RAII-based design patterns commonly used in embedded C++ development.

 

 

 

 

 

Loading...

Input

1 0

Expected Output

0 Object cleaned