Contrary to std::vector
and std::array
, std::unique_ptr
can own a NULL pointer.
This comes in handy when working with C APIs that expect either an array or NULL:
void legacy_func(const int *array_or_null);
void some_func() {
std::unique_ptr<int[]> ptr;
if (some_condition) {
ptr.reset(new int[10]);
}
legacy_func(ptr.get());
}