C ++ removes an object

I have no problems with processing memory in a C ++ program, so in this case I would like to get advice:

I want to create a new Object in a class function, which is needed until the end of the program. As far as I know, if I use the new operator, I sometimes have to delete it. Whereas it should be initialized inside the class, when and how to finally remove it?

+4
source share
4 answers

Edit: using some kind of smart pointer is often a good idea, but I find it still important to have a clear understanding of manual memory management in C ++.

If you want the object in the class to remain until the end of the program, you can simply make it a member variable. From what you said, there is nothing to suggest you use new or delete here, just make it automatic. If you want to use new and delete for practice, you should read the constructors and destructors for the class (you can and will use new and delete outside the classes, but I'm trying to save this according to your question). Here I prepared earlier:

 class Foo { public: Foo(); // Default constructor. ~Foo(); // Destructor. private: int *member; } Foo::Foo() // Default constructor definition. { member = new int; // Creating a new int on the heap. } Foo::~Foo() // Destructor. { delete member; // Free up the memory that was allocated in the constructor. } 

This is a simple example, but it will hopefully help you. Please note that the variable will be saved only as long as the object is alive. If the object is destroyed or goes out of scope, the destructor will be called and the memory will be freed.

+1
source

I suggest smart idiom pointer

 #include <memory> struct X { void foo() { } }; std::share_ptr<X> makeX() // could also be a class member of course { return std::make_shared<X>(); } int main() { std::share_ptr<X> stayaround = makeX(); // can just be used like an ordinary pointer: stayaround->foo(); // auto-deletes <sup>1</sup> } 

If the pointer is really a static variable, you can replace unique_ptr (which works the same way, but transfers ownership of the assignment, which means that the pointer should not contain a reference count)

Note To learn more about C ++ smart pointers in general, see smart pointers (boost) explained

Note If you do not have TR1 / C ++ 0x support for this, you can simply use Boost Smartpointer


<sub> 1 if you don’t miss copies of shared_ptr itself; this would be a weird use of previously invisible smart pointers :)

+5
source

You can use a smart pointer, as suggested by Sehe, or you can create a static object in a function and return a link to it. You should not explicitly delete the object, when the process ends, the object will be deleted. How:

 struct X {}; X& makeX() // could also be a class member of course { static X x; return x; } int main() { X& stayaround = makeX(); } 
+2
source

On most operating systems (Linux in particular), if you select a pointer to a new Object and do not bother with delete -ing, because you will need it until the program finishes, there is really no harm, your program has a memory leak ( you can use valgrind to look for such leaks), but the kernel will free up all the memory used when it runs out.

A better alternative is to have a singleton class for the application data, for example QApplication in Qt, ahd will build one instance of this class at the beginning of your main , and this class contains a smart or dumb pointer to your Object . The destructor must delete this object.

0
source

Source: https://habr.com/ru/post/1383630/


All Articles