Search code examples
cmemorymemory-managementstack-overflow

How can i remove this Segmentation fault in C Program


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.


Solution

  • 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.