函数结束时是否会释放其局部变量? [英] When a function ends are its local variables deallocated?

查看:346
本文介绍了函数结束时是否会释放其局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不是,这表示如果我想防止100%的内存泄漏,必须删除所有局部变量来结束每个函数?

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?

推荐答案

所有对象都有关联的存储持续时间.存储期限描述了对象的存储空间保留了多长时间.不是引用的局部变量会引入具有自动存储持续时间的对象,这意味着这些对象的存储会在其作用域结束时自动销毁.

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.

引用类型变量不会引入对象,甚至可能不需要存储它们自己的对象,但是它们仍然具有存储持续时间(第3.7/3节).如果参考书确实需要存储,则会根据参考书的存储期限将其释放.

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.

因此,任何类型的局部变量声明都不会泄漏.实际上,您不能delete具有自动存储持续时间的对象.这仅用于具有动态存储持续时间的对象,该对象使用new分配.

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.

这篇关于函数结束时是否会释放其局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆