What happens to heap in case of shared_ptr | Sololearn: Learn to code for FREE!


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?

9/17/2021 3:34:26 PM

Ketan Lalcheta

1 Answer

New Answer


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