2

Just curious, why is the default stack size in Visual Studio for C++ just 1MB? With modern computers, 1MB is extremely small. Is there a problem with changing my settings to something like 20MB?

coustyx
  • 63
  • 5
  • 1 MB usually is enough. Maybe you should rethink some class designs? Just a guess, but maybe turn some arrays into `std::vectors`? – Baum mit Augen May 04 '15 at 23:17
  • 1
    I'm using vectors now but the code that needed more stack space is using a cumbersome recursive algorithm. – coustyx May 04 '15 at 23:20
  • @coustyx Then turn your recursive algorithm into an iterative one. [It is possible](http://stackoverflow.com/questions/931762/can-every-recursion-be-converted-into-iteration) at least, though probably hard. – Baum mit Augen May 04 '15 at 23:24
  • 1MB ought to be enough for anybody. – Elliot Sep 29 '20 at 14:53

1 Answers1

2

Even if a resource is plentiful, one should only use as much as one actually needs.

1MB is plenty large for the vast majority of applications, which makes it a very reasonable default. Since some programs may need more (e.g. deeply recursive algorithms), you are free to change the default for your program.

Most functions will have the return address on the stack, parameters passed in (less some that may be passed via registers), as well as variables local to the function. Unless large variables are declared on the stack and/or the program is deeply recursive, this will add up to a few bytes to perhaps a few KB in typical cases.

Keep in mind that there can be many threads running on some systems, so routinely allocating much larger stacks can add up to a meaningful amount of resources.

I'm using vectors now but the code that needed more stack space is using a cumbersome recursive algorithm

Keep in mind that any recursive algorithm can be expressed as an iterative algorithm and vice-versa (sometimes a user-defined stack structure is required).

Eric J.
  • 147,927
  • 63
  • 340
  • 553