How to allow more memory and avoid on multiple recursion?

I am testing the time of an algorithm that makes many recursive calls. My program dies with approximately 128,000 recursive calls, and it only takes 0.55 seconds. I would like more analysis time in my analysis. I am running linux and using gcc. Is there a system call or environment variable or gcc flag or shell or something else?

+3
source share
5 answers

In gcc on Linux, there is no option to compare stack size. However , this text discusses how to set the stack size on Linux. using the command ulimit.

+10
source

, .

, , - . , .

, .

UP , 1 . , , , , - .


:

:

unsigned fact(unsigned x)
{
  if(x == 1)
    return 1;

   //--> Not tail recursive, multiply operation after the recursive call
   return fact(x-1) * x;
}

:

unsigned fact(unsigned x)
{
    return tail_fact(x, 1);
}

unsigned tail_fact(unsigned x, unsigned cur_val)
{
  if(x == 1)
    return cur_val;  

  return tail_fact(x-1, x * cur_val);  
}
+28

:

  • , . , .

  • , . , , .

  • , . Visual ++ . gcc .

+3

, , , ( " ... " ).

, :

int recursive_function(int x)
{
    if (1 == x)
        return 1;
    int scratchpad[100];
    ... // use scratchpad
    return recursive_function(x-1) + scratchpad[x-1];
}

, ( , 100), , - , , , .

, scratchpad 400 ( 32- , 800 64- ) recursive_function(), , recursive_function() 100 , 40 000 ( 80 000 64- ) , , :

int recursive_function(int x, int* buffer, int buffer_length)
{
    if (1 == x)
        return 1;
    ... // use buffer (and buffer_length to avoid overflow)
    int temp_value = buffer[x-1];
    return recursive_function(x-1, buffer, buffer_length) + temp_value;
}

, std::vector, , ( [. ], , ).

40k 80k , . , , ( , , , ).

, . , . , .


: STL, , . , ; , , , . , , - , : STL, , , .

"", , ( - ), , , , , . , std::vector , .

+3

setrlimit():

RLIMIT_STACK
This is the maximum size of the original stack stream, in bytes. Implementation does not automatically increase the stack above this limit. If this limit is exceeded, it SIGSEGVmust be generated for the stream. If the thread blocks SIGSEGVor the process ignores or catches SIGSEGVand does not agree on the use of an alternative stack, the location SIGSEGVmust be set to SIG_DFLbefore it is created.

+1
source

Source: https://habr.com/ru/post/1706323/


All Articles