#include <iostream>
#include <cstdint>
#include <memory>
using namespace std;
// Function creates and initializes the buffer,
// then transfers ownership to the caller.
unique_ptr<uint8_t[]> createBuffer(int n) {
unique_ptr<uint8_t[]> buf(new uint8_t[n]);
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
buf[i] = static_cast<uint8_t>(temp);
}
// Ownership moves to the caller via return value
return buf;
}
int main() {
int N;
cin >> N;
// main now owns the buffer
unique_ptr<uint8_t[]> buffer = createBuffer(N);
for (int i = 0; i < N; i++) {
cout << (int)buffer[i];
if (i != N - 1) cout << " ";
}
cout << endl;
return 0;
}
Explanation & Logic Summary:
The createBuffer function owns the buffer it creates. By returning a std::unique_ptr, ownership is transferred safely to the caller. No copying occurs, and no raw pointer is exposed. Cleanup is handled automatically when the unique_ptr in main goes out of scope. Numeric constraints ensure safe use of an 8-bit buffer.
Firmware Relevance & Real-World Context:
In firmware and embedded systems, memory ownership must be explicit to avoid leaks and undefined behavior. Using std::unique_ptr for ownership transfer mirrors real-world driver, HAL, and subsystem boundaries where one module allocates a resource and another consumes it safely.
Input
1 0
Expected Output
0