Thread: Recursion && given stack memory challenge.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2024
    Posts
    25

    Recursion && given stack memory challenge.

    Hi ,
    I studied recursion on c programming and I have one concern about stack memory handling on recursive calls while recursive function is getting executed. specifically how things are done on stack memory ..

    assume given this recursive function as following :

    Code:
    void recursion(int num) {
    
    
        if (num == 1 || num < 1 )
         {
           return ;
          }
         recursion(num/10);
    }
    assume num given as input 100 to the function, as well as we have stack
    memory given from 0 - 100 bytes , meaning that there is given implicitly integer array where its size from index 0 to 100, lets say given
    Code:
     stackmemory[100] 
    where recursion function work upon it.


    Now my concern is how I can by recursive calls keep tracking that I didnt arrive the maximum of stack memory? for not reaching stack over flow ... meaning how can I be sure that on the next recursive call that there is enough stack memory for next recursive call and if there is no sufficient space left on stack memory
    then recursion shall stop.

    How I can keep tracking that? I was thinking about pointers to save/store some pointers aside and ampersend "&" related somehow to tackle that issue.


    any idea or any help to tackle this issue? by the way the original function (given above recursion function) can be edited and to add any other additional functions ..


    Thanks.
    Last edited by AkamiKhmenisky; 01-25-2024 at 04:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursion and stack - trying to understand
    By bos1234 in forum C Programming
    Replies: 2
    Last Post: 09-01-2013, 01:58 AM
  2. Replies: 9
    Last Post: 04-29-2011, 09:26 AM
  3. Stack overflow in recursion
    By Nom1fan in forum C Programming
    Replies: 4
    Last Post: 11-24-2010, 12:51 PM
  4. Callback recursion: stack overflow
    By nicoqwertyu in forum C++ Programming
    Replies: 8
    Last Post: 03-16-2010, 11:09 AM
  5. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM

Tags for this Thread