Converting recursive function to tail recursive

This is a discussion on Converting recursive function to tail recursive within the C Programming forums, part of the General Programming Boards category; Please delete this thread, I have found the solution. Sorry for the inconvenience...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2010, 12:54 AM
  2. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 09:13 AM
  3. merge sort: recursive is fasfter than non-recursive
    By rio_cat in forum C Programming
    Replies: 8
    Last Post: 12-03-2006, 11:52 PM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21