+ 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?

25th Nov 2018, 7:18 AM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
11 Respuestas
+ 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.
25th Nov 2018, 7:33 AM
qwerty
qwerty - avatar
+ 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.
25th Nov 2018, 7:24 AM
qwerty
qwerty - avatar
+ 3
Does it same with make something on stack? It was destroyed when it gets out from the scope too..
25th Nov 2018, 7:26 AM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 3
qwerty does heap have limited size like stack?
25th Nov 2018, 7:49 AM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 3
Thanks for your answers and your explanation qwerty..
25th Nov 2018, 7:54 AM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 2
So why we need to allocates on memory (heap) rather than on stack
25th Nov 2018, 7:28 AM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 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.
25th Nov 2018, 7:31 AM
qwerty
qwerty - avatar
+ 2
Good Question
26th Nov 2018, 3:40 PM
Shivank Singh
Shivank Singh - avatar
+ 1
Zarthan Depends on how big your RAM is.
25th Nov 2018, 7:53 AM
qwerty
qwerty - avatar
0
Hello
26th Nov 2018, 9:38 AM
DASTURCHI DASTURLASH
DASTURCHI DASTURLASH - avatar
0
C++ Yes
26th Nov 2018, 9:38 AM
DASTURCHI DASTURLASH
DASTURCHI DASTURLASH - avatar