When do you worry about stack size?

前端 未结 19 990
难免孤独
难免孤独 2020-12-10 12:41

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 o

相关标签:
19条回答
  • 2020-12-10 13:39

    You worry about it when you write a callback that will be called from threads spawned by a runtime you don't control (for example, MS RPC runtime) with stack size at the discretion of that runtime. Somehow like this.

    0 讨论(0)
  • 2020-12-10 13:41

    Played this game a lot on Symbian: when to use TBuf (a string with storage on the stack), and when to use HBufC (which allocate the string storage on the heap, like std::string, so you have to cope with Leave, and your function needs a means of failing).

    At the time (maybe still, I'm not sure), Symbian threads had 4k of stack by default. To manipulate filenames, you need to count on using up to 512 bytes (256 characters).

    As you can imagine, the received wisdom was "never put a filename on the stack". But actually, it turned out that you could get away with it a lot more often than you'd think. When we started running real programs (TM), such as games, we found that we needed way more than the default stack size anyway, and it wasn't due to filenames or other specific large objects, it was due to the complexity of the game code.

    If using stack makes your code simpler, and as long as you're testing properly, and as long as you don't go completely overboard (don't have multiple levels of file-handling functions which all put a filename on the stack), then I'd say just try it. Especially if the function would need to be able to fail anyway, whether you're using stack or heap. If it goes wrong, you either double the stack size and be more careful in future, or you add another failure case to your function. Neither is the end of the world.

    0 讨论(0)
  • 2020-12-10 13:45

    You care about it on a microcontroller, where you often have to specify stack space explicitly (or you get whatever's left over after RAM gets used for static allocation + any RAM program space).

    0 讨论(0)
  • 2020-12-10 13:45
    1. When the code you've written for a PC suddenly is supposed to run on a mobile phone
    2. When the code you've ported to run on a mobile phone suddenly is supposed to run on a DSP

    (And yes, these are real-life snafus.)

    0 讨论(0)
  • 2020-12-10 13:46

    You usually can't really have large objects on the stack. They almost always use the heap internally so even if they are 'on the stack' their data members are not. Even an object with tons of data members will usually be under 64 bytes on the stack, the rest on the heap. The stack usually only becomes an issue these days when you have lots of threads and lots of recursion.

    0 讨论(0)
  • 2020-12-10 13:47

    When do you worry about stack size?

    Never.

    If you have stack size problems it means you're doing something else wrong and should fix that instead of worrying about stack size.
    For instace:

    • Allocating unreasonably large structures on the stack - don't do it. allocate on the heap.
    • Having a ridiculously long recursion. I mean in the order of painting an image and iterating over the pixels using recursion. - find a better way to do it.
    0 讨论(0)
提交回复
热议问题