Giuliano
Giuliano

Reputation: 192

About automatic or manual deletion of vectors

I read other questions trying to figure this out but I didn't find much like what I have in mind. I'm kinda new to C++ and I just don't want to have my C++ code to look like "C with classes", or in other words, if there is a good "C++ way of doing this", that's what I'm looking for, but I'm a bit confused. Well, to the problem.

I have a class, say MyClass. Now, in other class, say MyContainer, I have a vector<MyClass> vec.

Let's say I'm now creating a method that populates a MyClass object with any arbitrary stuff and then pushes it into vec. First question is: should I allocate and push the object like the following? (using vector<MyClass> vec)

MyClass *obj = new MyClass(args);

vec.push_back(*obj);

I am not sure this is right, but by my understanding, this could make me avoid having to manually delete every instance of MyClass I put into vec when the instance of MyContainer is of no use anymore.

What bugs me is that, like I said, this does not seem right anyway, and the other way I can figure out is declaring vector<MyClass*> and writing:

MyClass *obj = new MyClass(args);

vec.push_back(obj);

But this would make me write a method to delete every instance of MyClass I created before, right?

So... What should I use and, if both are actually right, when one is preferrable to the other?

However, if everything is wrong from the start, how can I do this?

Thank yall!

PS: by the way, MyContainer is created in main(). I'm planning to use heap allocation. Should I?

Edit: I can't use C++11.

PS2: This is part of an assignment, but I found it general enough to be of use for anyone, not just me.

Upvotes: 0

Views: 57

Answers (2)

sbaker
sbaker

Reputation: 537

Whether or not you should allocate on the heap or the stack as suggested by @happydave is highly dependent on the nature of "MyClass", IMO. If you need any kind of polymorphic behavior (i.e. you might have specialized subclasses of MyClass), then you should have a container of pointers.

If you're concerned about needing to delete each of the entries individually, there are two things that might help you:

1 . Use shared_ptr. So your code might look like this:

typedef std::shared_ptr<MyClass> MyClassPtr;

...

vector<MyClassPtr> vec;
vec.push_back(MyClassPtr(new MyClass());

The smart pointer will automatically delete MyClass when there are no more references to it.

2 . If you use bare pointers, you can still delete all the pointers with one line of code using std::for_each. This is described here: Can I call `delete` on a vector of pointers in C++ via for_each <algorithm>?

If you don't need polymorphic behavior and your class is copyable (either you gave it a copy constructor or the default copy is good for your implementation), then you can allocate on the stack.

Upvotes: 4

happydave
happydave

Reputation: 7187

You probably shouldn't use new unless you need the object to survive beyond the local scope of the variable.

Just declare it on the stack:

MyClass obj(args);
vec.push_back(obj);

In C++11, you can avoid any risk of extra copies of obj by using:

vec.emplace_back(args);

Upvotes: 4

Related Questions