What happens to heap in case of shared_ptr | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What happens to heap in case of shared_ptr

Hi I have overloaded new operator to observe allocation on heap. Please find attached code for same. https://code.sololearn.com/cCMGLKJyGjfd When object of shared_ptr is created for int type, it is expected to allocate 4 bytes on heap . Why another allocation of 24 bytes is done?

17th Sep 2021, 3:34 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 6
The additional 24 bytes are the allocation of the shared_ptr's control block, which stores all information necessary to manage its pointer correctly. See the following link for more information (needs some scrolling down the site): https://en.cppreference.com/w/cpp/memory/shared_ptr
17th Sep 2021, 4:15 PM
Shadow
Shadow - avatar