Recurssion: Why one crashes ?

Discussion in 'C Programming' started by codefixer@gmail.com, Sep 29, 2005.

  1. Guest

    Hello:

    I am trying to understand why one of these crash while the other works
    fine.
    long recurssion(int t)
    {
    if(t <= 1)
    return(1);
    else
    return(t * recurssion(t--)); //crashes
    }

    long recurssion(int t)
    {
    if(t <= 1)
    return(1);
    else
    return(t * recurssion(t-1)); //works fine
    }

    Thanks.
     
    , Sep 29, 2005
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > Hello:
    >
    > I am trying to understand why one of these crash while the other works
    > fine.
    > long recurssion(int t)
    > {
    > if(t <= 1)
    > return(1);
    > else
    > return(t * recurssion(t--)); //crashes


    return(t * recurssion(--t));

    > }
    >
    > long recurssion(int t)
    > {
    > if(t <= 1)
    > return(1);
    > else
    > return(t * recurssion(t-1)); //works fine
    > }


    The expressions 't--' and 't-1' do not have the same
    value. Read about 'post-increment operator' and
    'pre-increment operator'.

    -Mike
     
    Mike Wahler, Sep 29, 2005
    #2
    1. Advertising

  3. Mike Wahler wrote:
    > <> wrote in message
    > news:...
    > > return(t * recurssion(t--)); //crashes

    >
    > return(t * recurssion(--t));


    Both invoke undefined behaviour as they reference t for a purpose
    other than to calculate it's new value prior to the next (guaranteed)
    sequence point.

    --
    Peter
     
    Peter Nilsson, Sep 29, 2005
    #3
  4. writes:
    > I am trying to understand why one of these crash while the other works
    > fine.
    > long recurssion(int t)
    > {
    > if(t <= 1)
    > return(1);
    > else
    > return(t * recurssion(t--)); //crashes
    > }
    >
    > long recurssion(int t)
    > {
    > if(t <= 1)
    > return(1);
    > else
    > return(t * recurssion(t-1)); //works fine
    > }


    Add this as the first statement of each function and try running it:

    printf("Entering recurssion, t = %dl\n", t);

    (BTW, the correct spelling is "recursion".)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 29, 2005
    #4
  5. wrote:
    >
    > Hello:
    >
    > I am trying to understand why one of these crash while the other works
    > fine.

    [...]
    > return(t * recurssion(t--)); //crashes

    [...]
    > return(t * recurssion(t-1)); //works fine

    [...]

    What is the value of "t--" as compared to "t-1"?

    For example, given "x = recurssion(37);", what will be the value passed
    to the first recursion? What will the first pass to the second? And so
    on.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Sep 29, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul
    Replies:
    12
    Views:
    980
  2. Replies:
    2
    Views:
    1,069
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    986
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,237
    Smokey Grindel
    Dec 2, 2006
  5. Nemo

    Why delete operator crashes

    Nemo, Feb 23, 2006, in forum: C Programming
    Replies:
    7
    Views:
    303
    Default User
    Feb 23, 2006
Loading...

Share This Page