When a function ends are its local variables deallocated?

前端 未结 4 1366
日久生厌
日久生厌 2021-01-02 05:20

If not does that mean that I would have to end each function by deleting all local variables if I wanted to prevent 100% of memory leak?

4条回答
  •  长发绾君心
    2021-01-02 05:50

    All objects have an associated storage duration. A storage duration describes how long the storage for an object is kept around. Local variables that are not references introduce an object with automatic storage duration, which means that the storage of those objects is automatically destroyed at the end of its scope.

    Reference type variables do not introduce an object and may not even require storage of their own, but they still have storage duration (§3.7/3). If a reference does require storage, it will be released according to the reference's storage duration.

    As such, any kind of local variable declaration will not leak. In fact, you cannot delete an object with automatic storage duration. That is only used for objects with dynamic storage duration, which are allocated using new.

提交回复
热议问题