1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Compared with std::shared_ptr(new T), what does…

Added 30 Nov -0001

Compared with std::shared_ptr<T>(new T), what does std::make_shared<T>() change about allocation?

  1. It performs one allocation for both the object and the control block Correct answer
  2. It performs two allocations but avoids any virtual dispatch
  3. It keeps the control block on the stack, so only the object is heap based
  4. It uses a separate allocator, so the object and control block never free

Correct answer

A. It performs one allocation for both the object and the control block

14 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs