I have this smart pointer in top of my cpp file (Global variable):
std::unique_ptr<DATA_READ> smartPT(new DATA_READ);
What happens if smart pointer declares in global scope? I know smart pointer in a function automatically deletes and release memory after the function ends but How about Global Scope Smart Pointer which used in multiple functions?
It will release the allocated memory during the termination of the program. However, it is not a good idea to have smart pointer as global variable.
The memory will remain allocated throughout the life of the program, unless specific action is taken to free the memory. Essentially it will be just as if the scope for the smart pointer is the scope of the function 'main()'. Here is from cplusplus.com
unique_ptr objects automatically delete the object they manage (using a deleter) as soon as they themselves are destroyed, or as soon as their value changes either by an assignment operation or by an explicit call to unique_ptr::reset.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With