什么时候最好使用堆栈而不是堆,反之亦然? [英] When is it best to use the stack instead of the heap and vice versa?

查看:117
本文介绍了什么时候最好使用堆栈而不是堆,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,什么时候最好使用堆栈?什么时候最好使用堆?

In C++, when is it best to use the stack? When is it best to use the heap?

推荐答案

当当前函数返回后不使用变量时,使用堆栈。当在当前函数的生命周期之外需要变量中的数据时,使用堆。

Use the stack when your variable will not be used after the current function returns. Use the heap when the data in the variable is needed beyond the lifetime of the current function.

这篇关于什么时候最好使用堆栈而不是堆,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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