-3

I've been working on an implementation of the n-bodies problem, and I have to make a struct (asteroid) and a vector which contains those structs.

My question is, if I create the vector like this:

vector<asteroid> b(n_asteroids + n_planets);

And then I fill it like this:

for (it = 0; it < n_asteroids + n_planets; ++it){
  b[it] = {arg1, arg2, arg3...}
}

Do I need to call delete for either the asteroid structs or the vector? Or will the destructor free them when my program ends main?

Thanks!

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
Halo
  • 55
  • 1
  • 7

2 Answers2

2

Your vector has an automatic storage class which means the memory it occupies will be released when it will go out of scope. This means that the contents of the vector will be released too.

If the vector held pointers that pointed to dynamically then you would have the need in delete.

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • Yes, asteroid is a struct. So won't this cause memory leaks? – Halo Nov 19 '17 at 14:06
  • Not at all. Once the vector will "die" (go out of scope / execution terminated) the memory will be released. Note that this is not the case if you would have used pointers!! (i.e. `vector b(n_asteroids + n_planets);`) – CIsForCookies Nov 19 '17 at 14:08
  • The vector is already full of asteroids at the moment it is constructed. – juanchopanza Nov 19 '17 at 14:08
  • Right. Correcting... – CIsForCookies Nov 19 '17 at 14:08
  • *"constructed statically"* What does that even mean? – Baum mit Augen Nov 19 '17 at 14:10
  • opposite from dynamic. wrong term? – CIsForCookies Nov 19 '17 at 14:10
  • *"dynamic construction"* is not a thing either. I'm not quite sure what you are talking about tbh. – Baum mit Augen Nov 19 '17 at 14:11
  • not as in "dynamic construction" but as in "dynamic memory allocation". Isn't it what happens here? - not using `new` means no dynamic allocation -> static allocation (how else should I describe this?) – CIsForCookies Nov 19 '17 at 14:12
  • You are talking about automatic vs. dynamic storage duration it appears, which would, if stated correctly, indeed be the core concept to apply here. Your assertion *"not using new means no dynamic allocation"* is not quite true, though, as the allocation is just hidden in the resource handler. However, how the vector acquires the memory is irrelevant to this question anyways. – Baum mit Augen Nov 19 '17 at 14:17
  • Thanks for the reply! I indeed messed up this answer. By your last sentence on vectors you imply that each vector constructed may use dynamic memory? – CIsForCookies Nov 19 '17 at 14:19
  • 1
    @CIsForCookies Unfortunately, it even has to because swapping vectors may not invalidate iterators. But through the eyes of a normal user, that's an implementation detail that normally need not concern you. – Baum mit Augen Nov 19 '17 at 14:23
2

You don't need to delete your vector, as you didn't use new to allocate it. Regarding vector members, from http://www.cplusplus.com/reference/vector/vector/~vector/

This calls allocator_traits::destroy on each of the contained elements, and deallocates all the storage capacity allocated by the vector using its allocator.

So the answer is no for you case. But note that if you hold pointers in your vector, e.g. vector<asteroid *>, which you allocated with new, and you don't have any reference left to this pointer, you will need to manually delete it.

OriBS
  • 722
  • 5
  • 9