#include <iostream>
#include <cstdint>
#include <memory>
using namespace std;
// Function clearly consumes ownership
void consumeBuffer(unique_ptr<uint8_t[]> buf, int size) {
for (int i = 0; i < size; i++) {
cout << (int)buf[i];
if (i != size - 1) cout << " ";
}
cout << endl;
}
int main() {
int N;
cin >> N;
unique_ptr<uint8_t[]> buffer(new uint8_t[N]);
for (int i = 0; i < N; i++) {
int temp;
cin >> temp;
buffer[i] = (uint8_t)temp;
}
// Ownership transfer is explicit at call site
consumeBuffer(std::move(buffer), N);
// Caller no longer owns the buffer
if (buffer == nullptr)
cout << "No data" << endl;
else
cout << "Has data" << endl;
return 0;
}
Explanation & Logic Summary:
A function that takes std::unique_ptr by value clearly states that it consumes ownership.
The caller must use std::move to transfer ownership, making the handoff explicit.
After the call, the caller’s pointer becomes empty, enforcing correct ownership semantics through the type system.
Firmware Relevance & Real-World Context:
In systems and firmware software, functions often represent processing stages that fully consume a buffer. Expressing ownership transfer directly in the function signature prevents misuse, avoids accidental sharing, and makes APIs safer and easier to reason about in large embedded codebases.
Input
4 10 20 30 40
Expected Output
10 20 30 40 No data