Creating a C ++ Pointer Pointer Vector

In my C ++ code, I have an Object class equipped with an id field of type int. Now I want to create a pointer vector of type Object *. I tried first

vector<Object*> v; for(int id=0; id<n; id++) { Object ob = Object(id); v.push_back(&ob); } 

but this did not succeed, because here the same address is simply repeated n times. If I used the new operator, I would get what I want, but I would like to avoid the dynamic allocation of memory. Then I thought that I needed to somehow declare n different pointers before the for loop. The direct way to do this is to declare an array, so I did this:

 vector<Object*> v; Object ar[n]; for(int i=0; i<n; i++) { ar[i] = Object(i); } for(int i=0; i<n; i++) { v.push_back(ar+i); } 

Is it possible to get a memory leak if I do this? Also, in my opinion, considering declaring an array is a bit awkward. Are there other ways to create a pointer vector, but avoid manual memory management?

EDIT: Why do I need pointers instead of simple objects?

Well, I changed the initial situation a bit, because I thought that I could present the question in the simplest possible form. In any case, I still think that the question can be answered without knowing why I want a pointer vector.

Actually, I have

 Class A { protected: vector<Superobject*> vec; ... }; Class B: public A {...}; Class Superobject { protected: int id; ... } Class Object: public Superobject {...} 

In the derived class B I want to populate the vec member field with objects of type Object . If the superclass didn't use pointers, I would have problems with trimming objects. Therefore, in the class B constructor, I want to initialize vec as a vector of pointers of type Object* .

EDIT2

Yes, it seems to me that dynamic allocation is a reasonable option, and the idea of ​​using an array is a bad idea. When the array goes out of scope, everything will go wrong, because the pointers in the vector point to memory cells that do not necessarily contain objects anymore.

In the constructor for class B, I had

 B(int n) { vector<Object*> vec; Object ar[n]; for(int id=0; id<n; id++) { ar[id] = Object(id); } for(int id=0; id<n; id++) { v.push_back(ar+id); } } 

This caused a very strange behavior in class B objects.

+6
source share
3 answers

In this loop:

 for(int id=0; id<n; id++) { Object ob = Object(id); v.push_back(&ob); } 

You create n times an instance of the object on the stack. At each iteration, an element is created and deleted. You can simply avoid this by using this:

 for(int id=0; id<n; id++) { Object* ob = new Object(id); v.push_back(ob); } 

Thank you for each new item on the heap not on the stack. Try adding a constructor to the Object class like this:

 std::cout<<"Object ctor()\n"; 

and the same thing in the destructor:

 std::cout<<"Object dtor()\n"; 

If you do not want to create these objects using the "new" reason written by @woolstar

+6
source

Your question about memory leak makes me think that you are concerned about the life cycle and cleaning up of these objects. I originally offered shared_ptr wrappers, but C ++ 11 gave us unique_ptr , and C ++ 14 filled in the missing make_unique . So, with all that we can do:

 vector<unique_ptr<SuperObject>> v ; 

which you create on site with the wonder of perfect shipping and variable templates,

 v.push_back( make_unique<Object>( ... ) ) ; 

Yes, you have to live with some heap allocations, but everything will be cleared when v leaves.

Someone suggested the boost library, ptr_container , but this requires not only adding a boost to your project, but also teaching all future readers what ptr_container .

+5
source

There is no memory leak in your version. When the program leaves the scope, the array is also destroyed. To your second question: why not just store objects directly in a vector?

 vector<Object> v; for(int i = 0; i < n; i++) { Object obj = Object(i); v.push_back(obj); } 
+3
source

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


All Articles