c++smart-pointers

Smart pointer in global scope


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?


Solution

  • 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.