Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Recurssion: Why one crashes ?

Reply
Thread Tools

Recurssion: Why one crashes ?

 
 
codefixer@gmail.com
Guest
Posts: n/a
 
      09-29-2005
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.

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-29-2005

<> wrote in message
news: oups.com...
> 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


 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      09-29-2005
Mike Wahler wrote:
> <> wrote in message
> news: oups.com...
> > 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-29-2005
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) kst- <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.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      09-29-2005
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: <private.php?do=newpm&u=>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
James Gosling the Creator of EMACS and JAVA - leaves ORACLE - Butthen reports started coming in of odd failures. Systems would crashstrangely. We'd get crashes in applications. All applications. Crashes in thekernel. small Pox C Programming 2 07-22-2010 10:07 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
this stl code crashes, why??? Paul C++ 12 09-04-2004 05:34 AM
".NET Framework" makes crashes of my PC. why? Fogar Computer Information 3 04-18-2004 10:03 PM



Advertisments