#include <iostream>
#include <cstdint>
#include <memory>
using namespace std;
// The signature now explicitly demands ownership of the unique_ptr
void consumeBuffer(unique_ptr<uint8_t[]> buffer, size_t N) {
for (int i = 0; i < N; i++) {
// Cast to int so it prints the number, not an ASCII character
cout << (int)buffer[i];
if (i != N-1) cout << " ";
}
cout << endl;
// As soon as this function finishes, the buffer is automatically destroyed.
}
int main() {
int N;
cin >> N;
unique_ptr<uint8_t[]> buffer = make_unique<uint8_t[]>(N);
for (int i = 0; i < N; i++) {
int temp;
cin >> temp;
buffer[i] = (uint8_t)temp;
}
// Transfer ownership to the function using std::move
consumeBuffer(std::move(buffer), N);
// Because we moved it, buffer is now guaranteed to be empty
if (buffer == nullptr)
cout << "No data" << endl; // This will now print!
else
cout << "Has data" << endl;
return 0;
}
Input
4 10 20 30 40
Expected Output
10 20 30 40 No data