3 ms·
As I said, unique_ptr (or any of the others), do not check for null. So you can do this, and it will cause a segfault: std::unique_ptr<int> a; printf("
by ben-schaaf 8mo ago
As I said, unique_ptr (or any of the others), do not check for null. So you can do this, and it will cause a segfault:
std::unique_ptr<int> a;
printf("%d\n", *a);
Similarly unique_ptr::operator[] doesn't (and can't) do bounds checking, for example:
std::unique_ptr<int[]> a = std::make_unique<int[]>(2);
printf("%d\n", a[2]);
There's also no attempt to limit the construction of invalid smart pointers, for example:
int num;
std::unique_ptr<int> a(&num);
Smart pointers simplify memory management, and they're slightly harder to misuse than regular pointers, but they simply make no attempt at providing memory safety.