A shared_ptr is built as std::shared_ptr<int>(new int[10]). What happens when the last reference to it goes away?
Correct answer
A. Plain delete is called on the array pointer, which is undefined behaviour
20 views
A. Plain delete is called on the array pointer, which is undefined behaviour
20 views
std::shared_ptr stores a deleter chosen at construction, and the default one calls delete rather than delete[]. Constructing it from new int[10] therefore mismatches the allocation and deallocation forms, which is undefined behaviour. C++17 added shared_ptr<T[]> and make_shared<T[]> for the common case, and a custom deleter works on older standards.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.