Consider the following code snippet:
#include
using namespace std;
void sub(vector& vec) {
vec.push_back(5);
}
int main()
Also note that your vector (vec) is object itself. It resides on the stack and when this object goes out of scope (which is end of main in your case), it is destructed. Memory for elements is allocated during initialization of this object and released with its destruction, which is a lovely example of RAII idiom, since the resource management of elements is tied to the lifespan of vector object.