0

here i want to solve stack overflow issue in this code. here in this code i call function p recursively 350000 times so i got segmentation fault when i remove 350000 and put 300000 than it works fine here segmentation fault comes because of i calling recursively call of function p more times or calling a recursive function too deep.

this is not working because i take if(i != 350000). its stop at may be in this range 300000 to 327480. i tested 10 times

CODE:

#include <stdio.h>

void p(char *, int);

int main() 
{
    char *a = "HI";
    int b = 10;
    p(a, b);
    printf("\nComplete");
    return 0;

}
void p(char *a, int b) 
{
    static long int i = 0;

    if (i != 350000) 
    {
        printf("\n%ld \t at Hi hello", i);
        i++;
        p(a, b);
    } else 
    {
        return;
    }
}

when i check this code using valgrind tool than valgraind reporting error like this

==9236== Stack overflow in thread 1: can't grow stack to 0x7fe801ff8
==9236== 
==9236== Process terminating with default action of signal 11 (SIGSEGV)
==9236==  Access not within mapped region at address 0x7FE801FF8
==9236==    at 0x4EA012E: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1276)
==9236==  If you believe this happened as a result of a stack
==9236==  overflow in your program's main thread (unlikely but
==9236==  possible), you can try to increase the size of the
==9236==  main thread stack using the --main-stacksize= flag.
==9236==  The main thread stack size used in this run was 8388608.
==9236== Stack overflow in thread 1: can't grow stack to 0x7fe801ff0
==9236== 
==9236== Process terminating with default action of signal 11 (SIGSEGV)
==9236==  Access not within mapped region at address 0x7FE801FF0
==9236==    at 0x4A2269F: _vgnU_freeres (vg_preloaded.c:58)
==9236==  If you believe this happened as a result of a stack
==9236==  overflow in your program's main thread (unlikely but
==9236==  possible), you can try to increase the size of the
==9236==  main thread stack using the --main-stacksize= flag.
==9236==  The main thread stack size used in this run was 8388608.

Please help me guys i really want this issue solution. and i cant remove recursively call of function from my code.

3 Answers 3

3

Stacks are not an unlimited resource. Recursion lends itself well to algorithms which reduce their search space relatively quickly (binary chop of a sorted array, binary or multi-way tree traversal and so on).

If you find yourself with an algorithm that requires recursion to the level of three hundred and fifty thousand times, you really should reconsider using a recursive solution.

For example, something like:

def addUnsigned (a, b):
    if a == 0:
        return b
    return addUnsigned (a-1, b+1)

is not a good match for recursion.

If you really cannot remove the recursion, then you need to do what valgrind suggests, change the stack size. For example, the linkage editor on my system, ld, has a --stack option which allows you to specify the reserved (and,optionally, committed) stack size.

10
  • So how can i know the current stack size and reset the stack size programmatically
    – sam_k
    Nov 1, 2011 at 6:37
  • You don't do it in the program, it's generally done by the linker or loader. Seriously, you need to revisit your need for recursion.
    – paxdiablo
    Nov 1, 2011 at 6:41
  • Reply me...how can i do this?
    – sam_k
    Nov 1, 2011 at 6:42
  • if you use MACRO instead of thet function then ? Nov 1, 2011 at 6:52
  • Sam, you need to look into the options for your linker. If you're using ld, try ld --help to get the options. You'll probably find something there that can specify stack size. If you're asking how to turn a recursive solution into an iterative one, that's best asked in a separate question.
    – paxdiablo
    Nov 1, 2011 at 7:26
1

With that particular example, if you turn up the optimisation level of your compiler sufficiently high it should perform tail call elimination, which will solve the stack overflow problem (for example, gcc 4.4.5 does so at -O2 and above).

0

If you still want to control it programmatically, you can call your function in a thread and set the stack size for the thread (and wait till the thread completes).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.