Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: Amit Saha <amitsaha.in@gmail.com>
To: gdb@sourceware.org
Subject: Doubt regarding a tail optimized code under ‘gdb’
Date: Mon, 18 May 2009 16:47:00 -0000	[thread overview]
Message-ID: <547db2260905180946u3c7dbec5s830511b49e15947e@mail.gmail.com> (raw)

Hello all,

Out of sheer curiosity, I tried to run a tail optimised code under
'gdb'. Consider a tail recursive factorial implementation in C:

<code>

#include <stdio.h>

unsigned long long factorial(unsigned long long fact_so_far, unsigned
long long count, unsigned long long max_count){

if (max_count==0 || max_count==1 || count >= max_count)
        return fact_so_far;
else
{
        printf("%llu  %p \n", count, &factorial);
        return factorial(fact_so_far * count, ++count, max_count);
}

}

int main(int argc, char **argv)
{
        unsigned long long n;
        scanf("%llu", &n);
        printf("\n Factorial %llu \n",factorial(1,0,n));
        return 0;

}


</code>

I place a breakpoint in 'factorial' and I run the above under 'gdb'.
The breakpoint is never hit. Assuming that its tail call optimised (I
have compiled it using gcc -O2), it should hit the breakpoint, at
least once. I get the final result without hitting any breakpoint. For
eg,

(gdb) b factorial
Breakpoint 1 at 0x8048429: file factorial-tail.c, line 3.
(gdb) run
Starting program: /home/amit/quest/codes/factorial-tail
5
0  0x8048420
1  0x8048420
2  0x8048420
3  0x8048420
4  0x8048420

 Factorial 120

Program exited normally.
(gdb)

Where am I going wrong? Any pointers would be appreciated?  I am using
'gdb-6.8-debian' and gcc-4.3.3.


Best Regards,
Amit

-- 
Journal: http://amitksaha.wordpress.com
IRC: cornucopic on #scheme, #lisp, #math, #linux

"Recursion is the basic iteration mechanism in Scheme"
--- http://c2.com/cgi/wiki?TailRecursion


             reply	other threads:[~2009-05-18 16:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-18 16:47 Amit Saha [this message]
2009-05-18 17:43 ` Paul Pluzhnikov
2009-05-19  2:48   ` Amit Saha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=547db2260905180946u3c7dbec5s830511b49e15947e@mail.gmail.com \
    --to=amitsaha.in@gmail.com \
    --cc=gdb@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox