3

Let's consider a class that contains a vector:

AGivenClass
{

public:

 vector< int > vec_int;

};

and let's consider that we have an instance, which is a vector of the class :

vector < AGivenClass > vec_instance;

My question is : Is doing

vec_instance.clear()

enough to free memory, including for the previously filled vec_int internal to the class ?

walid barakat
  • 342
  • 1
  • 3
  • 12
  • 2
    Yes, it's enough. – HolyBlackCat Apr 07 '20 at 10:34
  • @HolyBlackCat : thank you so much. You may put your nice comment to the answer so that I could validate it. Thanks – Mathieu Krisztian Apr 07 '20 at 10:36
  • 1
    No, it's not enough. You also need to call `shrink_to_fit` – user7860670 Apr 07 '20 at 10:36
  • 1
    @user7860670 : sorry : could you comment a bit further ? – Mathieu Krisztian Apr 07 '20 at 10:37
  • and what about if I would have in the class a vector of *pointer* ? – Mathieu Krisztian Apr 07 '20 at 10:39
  • 3
    @MathieuKrisztian Please beware that there is a difference between the question "What do I need to call so that all memory is released?" and "What do I need to call so that there won't be a memory leak?". As long as you don't use raw `new` or similar stuff and with few exceptions (such as circular `std::shared_ptr` or C-style APIs) you never need to release memory manually in C++ to avoid memory leaks, but you need to if you want to free memory prematurely before objects leave their scope and are destroyed automatically. – walnut Apr 07 '20 at 10:40
  • @user7860670 your claim is wrong and misleading. When `vector` of `AGivenClass` is cleaned all instances of `AGivenClass` inside it are destroyed and as a result all memory of `vector< int >` is released. Probably you are thinking about memory associated with `std::vector`, but this was not a questioned. – Marek R Apr 07 '20 at 10:48

1 Answers1

2

vec_instance.clear() will destoy all the contained objects, but won't deallocate memory buffer used to store them. Vector capacity won't change. So you also need to call vec_instance.shrink_to_fit() to make sure that all the memory is deallocated. Vector capacity should become 0 afterwards.

user7860670
  • 32,142
  • 4
  • 44
  • 75
  • ok thank you. Bonus : and what if my class includes a vector of pointer : should I first delete the pointers before to clear my vector of instance ? – Mathieu Krisztian Apr 07 '20 at 10:41
  • 4
    But it is a non-binding request to reduce capacity() to size(), according to docs https://en.cppreference.com/w/cpp/container/vector/shrink_to_fit. – ks1322 Apr 07 '20 at 10:42
  • 1
    @MathieuKrisztian Bonus question:for raw pointers: that depends if you're really done with the objects the pointers point to or not. – Jabberwocky Apr 07 '20 at 10:43
  • 1
    @MathieuKrisztian it should be a vector of *smart* pointers so there will be a proper RAII style cleanup performed without you writing any additional code. – user7860670 Apr 07 '20 at 10:43
  • @Jabberwocky : yes I mean that I no more needs the pointer at this stage. *Should* I delete them, or is the vector::clear() enough ? I use *standard* pointers. – Mathieu Krisztian Apr 07 '20 at 10:44
  • 1
    @MathieuKrisztian every raw pointer you've got via `new` must be deleted via `delete`, so yes, in that case you need to delete them beforehand – Jabberwocky Apr 07 '20 at 10:45
  • @ks1322 In practice this request is always fulfilled, especially when the size is 0. – user7860670 Apr 07 '20 at 10:45
  • 3
    @MathieuKrisztian [You should not use naked `new`s](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). Always use `std::unique_ptr` or `std::vector` instead and you won't ever need to think about memory leaks. – walnut Apr 07 '20 at 10:47