Reputation: 15592
Suppose a std::vector is defined in a function (i.e., on stack) and the vector is re-allocated (like by inserting data). Will any part of the vector be in heap? if yes, will it leave any garbage in heap when done executing the function? Thanks.
Upvotes: 1
Views: 68
Reputation: 355217
Will any part of the vector be in heap?
Yes: all of the elements in a std::vector
are stored in a heap-allocated array.
Will it leave any garbage in heap when done executing the function?
No. The std::vector
container, like all of the Standard Library containers, is responsible for cleaning up any objects that it creates.
Note, however, that if you dynamically allocate objects (e.g., using new
) and store them in a container, then you are responsible for destroying those objects. To avoid having to clean things up yourself, you should avoid explicitly dynamically allocating objects wherever possible and use smart pointers everywhere else.
Upvotes: 2