About Smart pointer.. | Sololearn: Learn to code for FREE!


About Smart pointer..

Does std::unique_ptr is same with make class or variable on stack? And when is the best time to use std::unique_ptr?

11/25/2018 7:18:28 AM


11 Answers

New Answer


You need to allocate memory from the heap if: - Your object is too big - You're gonna allocate a bunch of objects - You're gonna make a large array - You just don't have enough space for the stack (A stack is just a few megabytes per app) You have to allocate from the heap if you should, but you don't have to manually call new and delete everytime you allocate from the heap. That's why smart pointers exist.


You should always use std::unique_ptr whenever you could. std::unique_ptr basically allocates memory for the object when you make one and it gets freed when it gets out of scope with a destructor.


Does it same with make something on stack? It was destroyed when it gets out from the scope too..


qwerty does heap have limited size like stack?


Thanks for your answers and your explanation qwerty..


So why we need to allocates on memory (heap) rather than on stack


Smart pointers allocate your object using the heap. So it isn't exactly the same as the stack. But yeah it get's destroyed when it gets out of scope.


Good Question


Zarthan Depends on how big your RAM is.