Suppose I have the following function:

int func(void) { std::vector<int> vec1; vec1.push_back(10); std::vector<int> vec2(vec1.begin(), vec1.end() ); return 0; } 

As I understand it, after calling vec1 and vec2, they are released from the stack as local variables. Is the heap freed from the arrays pointed to by vec1 and vec2?

    1 answer 1

    The memory allocated for storing the elements of the vector is freed, since local object destructor is called automatically when exiting the method

    RAII :

    If you want to follow up on your safety standards, you’ll find out how to manage your life. never throw), and don't require explicit cleanup.

    Object Destructor Vector :

    Destroys the container. After calling the destructor, the used memory is released. Note that if elements are pointers, the objects to which they point are not destroyed.