在堆栈上使用大变量/数组的缺点? [英] Disadvantages of using large variables/arrays on the stack?

查看:43
本文介绍了在堆栈上使用大变量/数组的缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在堆栈上定义大型数组或对象有哪些缺点(如果有的话)?以下面的例子:

What are the disadvantages, if any, of defining large arrays or objects on the stack? Take the following example:

int doStuff() {
   int poolOfObjects[1500];
   // do stuff with the pool
   return 0;
}

这有任何性能问题吗?我听说过堆栈溢出问题,但我假设这个数组不够大.

Are there any performance issues with this? I've heard of stack overflow issues, but I'm assuming that this array isn't big enough for that.

推荐答案

栈溢出是个问题,如果

  • 数组大于线程栈

  • the array is larger than the thread stack

调用树很深,或者

函数使用递归

在所有其他情况下,堆栈分配是一种非常快速有效的获取内存的方式.但是,调用大量构造函数和析构函数可能会很慢,因此如果您的构造函数/析构函数不重要,您可能需要查看寿命更长的池.

In all other cases, stack allocation is a very fast and efficient way to get memory. Calling a large number of constructors and destructors can be slow, however, so if your constructor/destructor are non-trivial, you might want to look at a longer-lived pool.

这篇关于在堆栈上使用大变量/数组的缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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