473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Recurssion: Why one crashes ?

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.

Nov 15 '05 #1
4 1650

<co*******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr 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
Nov 15 '05 #2
Mike Wahler wrote:
<co*******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr 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

Nov 15 '05 #3
co*******@gmail.com 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) ks***@mib.org <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.
Nov 15 '05 #4
co*******@gmail.com 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:Th*************@gmail.com>
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Steve D. | last post by:
I recently installed VS.NET 2003 After opening a project and leaving it open, after a certain period of time it crashes. It only does this when a project is open. The project that I've tried...
5
by: Rob Ristroph | last post by:
Hi, It's pretty unhelpful to post "I have a huge piece of code that crashes in strange places, what's the problem?" but that's basically my problem and I really am at my wit's end. The piece...
12
by: Paul | last post by:
Hi, Global operator new and delete are overloaded and I am using stl map to store pointers, but this code crashes, can some one shed some light??? Compiler: MS VC++ 6.0 STL: Shipped with...
9
by: greeningster | last post by:
I have written an application in Visual C++ for a customer but it seems to crash randomly. Could anyone give me any help on how I could track this down ? Also, there appears there might be...
0
by: H. Peter Lienhardt | last post by:
One of our app written in Access 2002 crashes frequently under Windows 98 - there is no such problem under NT, W2k or Windows XP. The problem starts with the Windows message 'There are not...
1
by: rogsonl | last post by:
The latest versoin of Microsoft Visual Studio 2005 (Version 8.0.50727.42 RTM.050727-4200 Microsoft .NET Framework Version 2.0.50727) crashes very often in debug. 1. If you modify a case statement...
22
by: walterbyrd | last post by:
I like to develop on my desktop, then when I get stuff working, I copy to my web-site. I set up a new version Xampp on my windows-2k desktop. And downloaded the stuff from the website to edit....
4
by: yinglcs | last post by:
Hi, I have a c++ application which crashes in this line (from the debugger, I have a segmentation fault here): void *object = dynamic_cast<void>(aObject); I have stepped thru the code in...
41
by: z | last post by:
I use Visual C 2005 to develop my programs. One in particular is crashing in very specific and hard to replicate situations, made worse by the fact it only crashes when run -outside- the dev - as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.