how to implement make_shared | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how to implement make_shared

Hi trying to implement shared_ptr in C++ as below: https://code.sololearn.com/c5a11A166a10/#cpp Can anyone suggest how to implement make_shared method? I am confused to start and specially in the case of passing argument to make_shared.

8th Jun 2021, 1:56 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 2
It's quite easy actually. I have implemented it here, and also left some comments that you should read https://code.sololearn.com/cS7OaVDPvHMk/?ref=app Also, shared pointers don't copy keep the counts and pointer to the object themselves and pass them around. Instead, they dynamically allocate an object that holds the pointer and the count, and then each shared pointer holds a pointer to that object. Jump to the implementation notes section in this link and read it (read Shadow's comment on my code for why a control block is used to mamage the pointer and count) https://en.cppreference.com/w/cpp/memory/shared_ptr
8th Jun 2021, 4:17 PM
XXX
XXX - avatar