5

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?

Ali Sepehri-Amin
  • 493
  • 1
  • 6
  • 18
  • 3
    A global smart pointer will release the memory at the end of the program. Very much like a non-smart, non-pointer global variable. – Bo Persson Oct 11 '17 at 19:47
  • It will be destructed when the application exits - see [this question](https://stackoverflow.com/questions/246564/what-is-the-lifetime-of-a-static-variable-in-a-c-function) for more details – Steve Lorimer Oct 11 '17 at 19:47
  • It's destructor gets called after main() returns – stijn Oct 11 '17 at 19:48

4 Answers4

1

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.

MJ_Wesson
  • 31
  • 5
1

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.

Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52
0

Since this is a variable with static duration, the memory will be allocated when this code is loaded, usually that will be on start of your application and freed, when the application finishes. If you use it in functions it should generally be allocated, unless it has been reset in another function.

Obviously there are some ramifications considering dynamically loaded libraries.

-1

The smart pointer will be destroyed at the end of the program as all other objects. So when the destructor is called the pointer will be deleted. Her you get an example which is not even close to a real smart pointer, but it gives the idea:

#include <iostream>
using namespace std;

template <typename T>
struct example {
    T* p_;
    example(T* p): p_{p} {
        cout << "example(T* p)\n";
    }
    ~example() {
        cout << "~example()\n";
        delete p_;
    }
};

int main() {
    cout << "start main\n";
    example<int> p{new int};
    cout << "end main\n";
    return 0;
}

Try it out here: https://ideone.com/rOtQY9

Anyway, the use of a global smart pointer eludes me. The program is finished so the memory would have been released to the OS anyway. The nice thing is that whatever resource has been acquired during construction will also be released for sure (e.g. a file could be closed correctly, or a bitstream buffer could be flushed).

Costantino Grana
  • 3,132
  • 1
  • 15
  • 35
  • So a smart pointer is only needed, if the OS has no virtual memory management system. – Tom Aug 10 '21 at 19:27
  • @Tom No, virtual memory has nothing to do with smart pointers. You use a smart pointer when your pointer needs to be deleted in a different context with respect to where it was allocated. If it is a `unique_ptr<>` there is a single owner and you can pass ownership around without worrying about who is in charge of deleting it (the only owner will delete the pointer). If it is a `shared_ptr<>` many objects can share the memory but writing down when they are done with it. When you are the last one, you delete the pointer. – Costantino Grana Aug 10 '21 at 21:22
  • I know, what a smart pointer is. But will the resource of a global non-smart pointer be deleted at program exit? – Tom Aug 12 '21 at 00:01
  • @Tom No, there is no guarantee that it will happen. Specifically, under Linux and Windows, you are sure that any memory allocated by your program will be freed, but its *outside* the C++ competence. The destructor of the object possibly pointed by the global non-smart pointer will not be called. – Costantino Grana Aug 12 '21 at 12:18