destruct of container

1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include<vector>
using namespace std;


int main(){
    vector<int> v1{1,2,3,34,5};
  v1.~vector();
    return 0;
}

this programm Exited with error status 134.
Acroodding to the message complier sent, it seems that i have made a mistake of double free.but i have just called the destructed function once only.
i wonder why and how to use"c.~container()" correctly.
appreciate it if you can do me a favour.
There is no need to call the dtor by yourself. Once the vector gets out of scope the dtor gets called automatically.
ok,but i wonder if it is no need for programmer to dtor the vector(in that case). and in what case should we destruct the container by ourselves?
in what case should we destruct the container by ourselves?


You don't. The class destructor will always be called as needed when the class instance goes out of scope.
There is a caveat I'd like to add,
Vector is one of the Standard Template Library classes that help you with garbage collection and help you avoid memory leaks, so you can get away with it.. this time. BUT if you're using custom pointers outside of the STL classes, you would want to delete those pointers and set them to nullptr.


Last edited on
The topic is lifetime of an object and storage duration. See https://arne-mertz.de/2016/01/c-object-lifetimes/
Topic archived. No new replies allowed.