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 6474155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:33:12+00:00 2026-05-25T06:33:12+00:00

As far as I understand, a tail recursive function calls itself at the very

  • 0

As far as I understand, a tail recursive function calls itself at the very last step (like the return statement) however, the first instance of the function is not terminated until all the other instances are terminated as well, thus we get a stack overflow after a number of instances are reached. Given that the recursion is at the very last step, is there any way to terminate the previous instance during or right before the next instance? If the only purpose of an instance is to call the next one, there is no reason why it should reside in memory, right?

  • 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-25T06:33:13+00:00Added an answer on May 25, 2026 at 6:33 am

    Yes, some compilers will optimize tail recursion so that it doesn’t require extra stack space. For example, let’s look at this example C function:

    int braindeadrecursion(int n)
    {
      if (n == 0)
        return 1;
    
      return braindeadrecursion(n - 1);
    }
    

    This function is pretty easy; it just returns 1. Without optimizations, clang generated code on my machine that looks like this:

    _braindeadrecursion:
    00  pushq   %rbp
    01  movq    %rsp,%rbp
    04  subq    $0x10,%rsp
    08  movl    %edi,0xf8(%rbp)
    0b  movl    0xf8(%rbp),%eax
    0e  cmpl    $_braindeadrecursion,%eax
    11  jne 0x0000001c
    13  movl    $0x00000001,0xfc(%rbp)
    1a  jmp 0x0000002e
    1c  movl    0xf8(%rbp),%eax
    1f  subl    $0x01,%eax
    22  movl    %eax,%edi
    24  callq   _braindeadrecursion
    29  movl    %eax,%ecx
    2b  movl    %ecx,0xfc(%rbp)
    2e  movl    0xfc(%rbp),%eax
    31  addq    $0x10,%rsp
    35  popq    %rbp
    36  ret
    

    As you can see, the recursive call is in there at 0x24. Now, let’s try with higher optimization:

    _braindeadrecursion:
    00  pushq   %rbp
    01  movq    %rsp,%rbp
    04  movl    $0x00000001,%eax
    09  popq    %rbp
    0a  ret
    

    Now, look at that – no more recursion at all! This example is pretty simple, but the optimization can still occur on more complicated tail recursive cases.

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

Sidebar

Related Questions

As far as I understand, in Scala we can define a function with no
As far as I understand, this code grabs only the first shape in the
I understand so far that in Jquery, with html() function, we can convert HTML
As far as I understand, the reduce function takes a list l and a
As far as I understand, if I want to get my database under source
As far as I understand, binding to a var in ActionScript is carried out
As far as I understand, https is http plus SSL/TLS. What do I need
As far as I understand, the onchange event of a text input element is
As far as I understand the serial port so far, transferring data is done
As far as I understand WebRequest.PreAuthenticate is almost always good. If I enable it

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.