0
std::vector<int> v1 = {1, 2, 3, 4};
std::vector<int> v2 = {5, 6, 7, 8};

v1 = std::move(v2);

What happened to the memory allocated on the heap for v1. Has it been destroyed after the move assignment operator has been called? Or is there a potential memory leak here?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    The standard doesn't actually specify, but, in practise, `v1` takes charge of `v2`'s allocation and what `v1` was managing before is first destroyed. So, no memory leak. `v2` is left in an 'unspecified but consistent state' - usually, in practise, empty. – Paul Sanders Oct 03 '22 at 09:35
  • 1
    Related: [Is a moved-from vector always empty?](https://stackoverflow.com/questions/17730689/is-a-moved-from-vector-always-empty) – Jason Oct 03 '22 at 09:39
  • 3
    You can't make the standard containers leak so easily. – HolyBlackCat Oct 03 '22 at 09:40

0 Answers0