什么时候担心堆栈大小? [英] When do you worry about stack size?

查看:116
本文介绍了什么时候担心堆栈大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用允许对非常大的对象使用自动分配的语言编程时,何时以及如何担心堆栈大小?是否有任何推理堆栈大小的经验法则?

When you are programming in a language that allows you to use automatic allocation for very large objects, when and how do you worry about stack size? Are there any rules of thumb for reasoning about stack size?

推荐答案


允许您为非常大的对象使用自动分配的语言...

When you are programming in a language that allows you to use automatic allocation for very large objects ...

如果我想分配一个非常大的对象,在堆栈上我可以在堆上分配它,但是包装在一个 auto_ptr (在这种情况下,它会被释放,当它超出范围,就像堆栈居民对象,但不用担心堆栈大小)。

If I want to allocate a very large object, then instead of on the stack I might allocate it on the heap but wrapped in an auto_ptr (in which case it will be deallocated when it goes out of scope, just like a stack-resident object, but without worrying about stack size).


...什么时候和你如何担心堆栈大小? b $ b

... when and how do you worry about stack size?

我使用堆栈保守的习惯(例如大于大约512字节的任何对象被分配在堆上),我知道堆栈有多大(例如,默认情况下大约是兆字节),因此我知道我不需要担心它。

I use the stack conservatively out of habit (e.g. any object bigger than about 512 bytes is allocated on the heap instead), and I know how big the stack is (e.g. about a megabyte by default), and therefore know that I don't need to worry about it.


Are there any rules of thumb for reasoning about stack size?




  • 很大的对象可能会打击堆叠
  • b $ b
  • 非常深的递归可以打乱堆栈

  • 如果有很多线程并且正在运行,默认堆栈大小可能太大(占用太多的总内存)在有限内存嵌入式设备上,在这种情况下,您可能需要使用O / S API或链接器选项来减少每个线程的堆栈大小。

    • Very big objects can blow the stack
    • Very deep recursion can blow the stack
    • The default stack size might be too big (take too much total memory) if there are many threads and if you're running on a limited-memory embedded device, in which case you might want to use an O/S API or linker option to reduce the size of the stack per thread.
    • 这篇关于什么时候担心堆栈大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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