How to increase stack memory?

Duplicate How to allow more memory and avoid recursion?

I am writing a branch and related algorithm that has at least 10,000 levels of recursive function, but it does not work due to a bug. here is a simple example of my program in C ++:

void f(int k)
{
   if(k==10000) return;
   f(k+1);
} 

void main()
{
   f(1);
   return;
}

      

can anyone help?

0


a source to share


6 answers


This is a linker problem. You will need to tell the linker to increase the amount of memory allocated for the stack. It is different for different languages ​​and compilers. It can be a command line parameter, or it can be a configuration file, or it can even be specified in the source code.



+4


a source


If you are on Linux (Mac maybe too?) You can use the ulimit command.



But you might want to look at optimizing your algorithm, or look at tail recursion.

+2


a source


Any recursive algorithm can be rewritten as non-recursive using a list. So you've moved the issue from stack size to heap size, with heaps usually (much) larger than stack threads. There are also stack size linker flags, depending on your compiler / linker and platform.

0


a source


or you can rewrite recursion into intervention.

0


a source


Apart from the main question, you can use Valgrind and its Massif tool to profile your memory consumption of the stack (a bunch of array profiles are used by default, but can also be a stack profile if the option is enabled).

0


a source


If you are using _beginthreadex you can specify the stack size. I believe the default is 1MB. You can spin up a new thread to do your job and specify whatever stack size you want.

0


a source







All Articles