Compared with std::shared_ptr<T>(new T), what does std::make_shared<T>() change about allocation?
Correct answer
A. It performs one allocation for both the object and the control block
14 views
A. It performs one allocation for both the object and the control block
14 views
std::make_shared allocates the object and the reference-count control block in a single block, improving locality and halving the allocation count compared with constructing from a raw new expression. The trade-off is that the memory cannot be released while any weak_ptr still exists, because the object shares its allocation with the control block. make_shared also avoids the exception-safety gap between a new expression and the shared_ptr constructor.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.