Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6749273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:40:14+00:00 2026-05-26T12:40:14+00:00

here i want to solve stack overflow issue in this code. here in this

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:40:15+00:00Added an answer on May 26, 2026 at 12:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've decided to reopen this because I want to here more peoples views on
As I am an impressed reader of Stack Overflow I want to ask my
I'm not entirely sure if I can do what I want here, but I
I've managed to find the Regex to get almost the result I want here
I know, I am propably way out of line here :-) I want to
I work for a travel agency, they have a website, from here they want
I am having problems here if I want to check if eerste points to
Here's what I want to do: I want to allow the user to give
Here's what I want to do: Given a table PeopleOutfit (id int primary key,
Here's what I want the resulting class declaration to look like: public sealed partial

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.