Can I create an auto_ptr array?

I have a base class that is inherited by several derived classes. I want to create an autopointer array of a baseClass pointer. when I initialize this auto-pointer, I get some compile-time error, then I tried to do so.

std::auto_ptr<base>pbase[3]; std::auto_ptr<base> b1(new derived1()); std::auto_ptr<base> b2(new derived2()); std::suto_ptr<base> b3(new derived3()); pbase[0] = b1; pbase[1] = b2; pbase[2] = b3; 

It works fine, I fixed the memory leak problem, while I am one of the windows, I do not use valgrind, I use boost framework for leaks.

for compilation error:

 class A{ public: std::auto_ptr<base>pbase[2]; } 

in the A.cpp file

 A::A():pbase[0](new derived1()), pbase[1](new derived2()){ } 

I got error C2059:syntax error : '['

+4
source share
3 answers

std::auto_ptr deprecated since C ++ 11 and should not be used due to its weird copy behavior:

 std::auto_ptr<int> a(new int(1)); std::auto_ptr<int> b = a; // invalidates a! 

What he was trying to achieve is now purely solved by std::unique_ptr . Until the advent of semantics of relocation and rvalue references, this was not possible.

However, this does not seem to be a problem. From your example, it is possible that you are skipping memory because you are calling undefined behavior:

 pbase[3] = b3; // pbase is std::auto_ptr<base>[3] // pbase[0] - pbase[2] are valid indexes 

In fact, when I fix this problem and run valgrind --leak-check=full ./auto_ptr_app as a result, it tells me that no leakage is possible.

+7
source

I just ran two valgrind tests on the following code snippets:

First start:

 class Foo { public: Foo(){} virtual ~Foo(){} }; class Bar: public Foo { public: Bar(){} virtual ~Bar(){} }; int main(int argc, char** argv) { std::auto_ptr<Foo>arr[3]; std::auto_ptr<Foo> one(new Bar()); std::auto_ptr<Foo> two(new Bar()); std::auto_ptr<Foo> three(new Bar()); arr[0] = one; arr[1] = two; arr[2] = three; return 0; } 

Second run:

 class Foo { public: Foo(){} virtual ~Foo(){} }; class Bar: public Foo { public: Bar(){} virtual ~Bar(){} }; int main(int argc, char** argv) { std::auto_ptr<Foo> one(new Bar()); return 0; } 

While Valgrind does show possible memory leaks in both cases, the warnings displayed are exactly the same (the same number of warnings, the same warning text, the same stack) for both of them, pointing somewhere outside my code in linux.so files. Thus, we can assume that the way to use the auto_ptr array is fine. However, as pointed out in the comments, since C ++ 0x (which is the current C ++ standard), auto_ptr is considered deprecated due to its weird copy behavior (you can find more information, for example, in this article ). It is recommended to use std::unique_ptr .

Now, if you have some additional memory leaks in your code, then most likely this is due to your own classes. Unfortunately, you did not include them in the question, so we cannot say. In this case, you should check the constructors and destructors of your classes for memory leaks, or at least show us your classes. Another reason could be a typo in the index of the array that you have in the code ( pbase[3] , which is not related to bonds).

+1
source

Your problem is probably not in auto_ptr , although it is deprecated, and you should use unique_ptr or shared_ptr instead (if your compiler does not support C ++ 11, use boost::shared_ptr ). It seems that the leak is inside your class implementation. Check if there is a base virtual destructor, because you need it if you want to use the class polymorphically. Also check for all members of the base and derived classes and delete everything that you selected.

+1
source

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


All Articles