c++ deleting vector class member memory in destructor

前端 未结 4 2047
天涯浪人
天涯浪人 2021-01-19 16:42

I have a class containing a vector member variable. I know that vectors stored on the stack will be cleaned up (i.e. memory free\'d) when they go out of scope, but I\'m not

相关标签:
4条回答
  • 2021-01-19 17:22

    There is always ONLY ONE way to destroy an object, and that is by its destructor (in contrast, you can construct objects in several ways). STL containers were designed specifically to avoid such micromanagement of memory within data structures. If you are having to explicitly allocate or free memory in an STL container, you're doing it wrong.

    0 讨论(0)
  • 2021-01-19 17:23

    I think you need not call DTOR for the vector. The DTOR will be called as soon as the object goes out of scope, meaning the DTOR of fred is Called.

    0 讨论(0)
  • 2021-01-19 17:24

    The vector is cleaned up for you already! When a class gets destructed all of it's members destructors are also called. In this case v's destructor is called, which cleans up whatever it allocated.

    Non-static members' dtors are called when you reach the closing } of the destructor in the reverse order they were declared in. Then your base class destructor is called, if present.

    0 讨论(0)
  • 2021-01-19 17:24

    The destructor for the vector will be called when you destroy an instance of fred.

    0 讨论(0)
提交回复
热议问题