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

+18

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

ZΛRTHΛИ

11 Answers

New Answer

+8

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.

+4

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.

+3

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

+3

qwerty does heap have limited size like stack?

+3

Thanks for your answers and your explanation qwerty..

+2

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

+2

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.

+2

Good Question

+1

Zarthan Depends on how big your RAM is.

0

0