Thread: Converting recursive function to tail recursive

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Converting recursive function to tail recursive

    Please delete this thread, I have found the solution. Sorry for the inconvenience
    Last edited by ajacobs365; 10-30-2011 at 08:07 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It already looks like it is capable of tail recursion elimination.

    Indeed, gcc seems to think so.
    Code:
    $ gcc -S bar.c
    $ grep helper bar.s
    .globl count_helper
    	.type	count_helper, @function
    count_helper:
    	call	count_helper
    	.size	count_helper, .-count_helper
    $ gcc -S -O2 bar.c
    $ grep helper bar.s
    .globl count_helper
    	.type	count_helper, @function
    count_helper:
    	.size	count_helper, .-count_helper
    Closer inspection of the optimised code shows that a jump is used where there should be a call.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2010, 01:54 AM
  2. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  3. merge sort: recursive is fasfter than non-recursive
    By rio_cat in forum C Programming
    Replies: 8
    Last Post: 12-04-2006, 12:52 AM
  4. Recursive function
    By campermama in forum C++ Programming
    Replies: 6
    Last Post: 06-17-2004, 09:56 AM
  5. Algorithm help (Changing from Recursive to Non Recursive)
    By Thantos in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 07:27 PM