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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:42:39+00:00 2026-05-11T03:42:39+00:00

How do I tell if gcc (more specifically, g++) is optimizing tail recursion in

  • 0

How do I tell if gcc (more specifically, g++) is optimizing tail recursion in a particular function? (Because it’s come up a few times: I don’t want to test if gcc can optimize tail recursion in general. I want to know if it optimizes my tail recursive function.)

If your answer is ‘look at the generated assembler’, I’d like to know exactly what I’m looking for, and whether or not I could write a simple program that examines the assembler to see if there’s optimization.

PS. I know this appears as part of the question Which, if any, C++ compilers do tail-recursion optimization? from 5 months ago. However, I don’t think this part of that question was answered satisfactorily. (The answer there was ‘The easiest way to check if the compiler did the optimization (that I know of) is perform a call that would otherwise result in a stack overflow – or looking at the assembly output.’)

  • 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. 2026-05-11T03:42:39+00:00Added an answer on May 11, 2026 at 3:42 am

    Let’s use the example code from the other question. Compile it, but tell gcc not to assemble:

     gcc -std=c99 -S -O2 test.c 

    Now let’s look at the _atoi function in the resultant test.s file (gcc 4.0.1 on Mac OS 10.5):

            .text         .align 4,0x90 _atoi:         pushl   %ebp         testl   %eax, %eax         movl    %esp, %ebp         movl    %eax, %ecx         je      L3         .align 4,0x90 L5:         movzbl  (%ecx), %eax         testb   %al, %al         je      L3         leal    (%edx,%edx,4), %edx         movsbl  %al,%eax         incl    %ecx         leal    -48(%eax,%edx,2), %edx         jne     L5         .align 4,0x90 L3:         leave         movl    %edx, %eax         ret 

    The compiler has performed tail-call optimization on this function. We can tell because there is no call instruction in that code whereas the original C code clearly had a function call. Furthermore, we can see the jne L5 instruction, which jumps backward in the function, indicating a loop when there was clearly no loop in the C code. If you recompile with optimization turned off, you’ll see a line that says call _atoi, and you also won’t see any backward jumps.

    Whether you can automate this is another matter. The specifics of the assembler code will depend on the code you’re compiling.

    You could discover it programmatically, I think. Make the function print out the current value of the stack pointer (register ESP on x86). If the function prints the same value for the first call as it does for the recursive call, then the compiler has performed the tail-call optimization. This idea requires modifying the function you hope to observe, though, and that might affect how the compiler chooses to optimize the function. If the test succeeds (prints the same ESP value both times), then I think it’s reasonable to assume that the optimization would also be performed without your instrumentation, but if the test fails, we won’t know whether the failure was due to the addition of the instrumentation code.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer OK I got it. I'd installed Eclipse Classic, and it… May 11, 2026 at 11:58 pm
  • Editorial Team
    Editorial Team added an answer Use a GCHandle.Alloc. with GCHandleType.Pinned. You can then use GCHandle.AddrOfPinnedObject… May 11, 2026 at 11:58 pm
  • Editorial Team
    Editorial Team added an answer In the case of python, as this is IO bound,… May 11, 2026 at 11:58 pm

Related Questions

I am coding a basic traffic light control which will be displayed on the
How do I tell if my application (compiled in Visual Studio 2008 as Any CPU )
How do I tell if a page is SSL'd in classic ASP? I can't
How do I tell if an executable is a .NET application? I prefer not

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.