473,786 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

segmentation fault in recursion

#include <stdio.h>

int main()
{
int i;
for(i=1;i<=10;i ++)
main();
printf("C is urs..");
return 0;
}

Why it shows error segmentation fault ??
Jan 15 '08
14 2671
Keith wrote:
) CBFalconer <cb********@yah oo.comwrites:
)However a smart enough compiler could optimize it to:
)>
)#include <stdio.h>
)int main(void) {
) while (1) printf("C is urs..");
) return 0;
)}
)>
)(ignoring the failure to output any '\n's). The result should
)progress considerably faster.
)
) No, that would be an invalid optimization. In the original program,
) even assuming infinite resources, the printf call will never be
) reached.
)
) It could be optimized to:
)
) int main(void) {
) while (1);
) }
)
) The only difference in behavior would be the lack of memory exhaustion
) (a "stack overflow" in some implementations ).

I agree. A smart compiler could prove that the main() call never
returns, and optimize accordingly (it would first throw away all the
unreachable code after the first main-call, and then optimize away
the tail recursion).
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jan 17 '08 #11
On Jan 15, 1:49*pm, Eric Sosman <Eric.Sos...@su n.comwrote:
vipps...@gmail. com wrote:
On Jan 15, 10:09 pm, asit <lipu...@gmail. comwrote:
#include <stdio.h>
int main()
{
* * * * int i;
* * * * for(i=1;i<=10;i ++)
* * * * * * * * main();
* * * * printf("C is urs..");
* * * * return 0;
}
Why it shows error segmentation fault ??
Because you're a troll.

* * *You may be right about that.
As far as C is concerned, that is perfectly valid code.
perhaps a system limitation. C (or c.l.c) has nothing to do with that.

* * *The code is "valid" in that it expresses a computation.
Unfortunately, the computation will not complete in finite
time, nor will a finite machine have an easy time keeping
track of its intermediate states. *The inconvenient word
"finite" does in fact express a system limitation, but it's
a limitation that can be removed only with great difficulty
and at some expense.
I see no condition that will lead to termination.
This program will not terminate on an infinite machine with infinite
memory and an infinite number of CPUs.
It exhausts resources exponentially.
Jan 17 '08 #12
vippstar wrote:
On Jan 15, 10:09 pm, asit <lipu...@gmail. comwrote:
>#include <stdio.h>

int main()
{
int i;
for(i=1;i<=10;i ++)
main();
printf("C is urs..");
return 0;

}

Why it shows error segmentation fault ??

Because you're a troll.
As far as C is concerned, that is perfectly valid code.
perhaps a system limitation. C (or c.l.c) has nothing to do with that.
I remembered that there was a limit on the number of nested function calls
an implementation was required to support, but re-reading 5.2.4.1 shows
that it was a false memory. (It does say "127 nesting levels of blocks",
but I'm not sure whether that applies here.)
--
Army1987 (Replace "NOSPAM" with "email")
Jan 18 '08 #13
Keith Thompson wrote:
CBFalconer <cb********@yah oo.comwrites:
[...]
>asit <lipu...@gmail. comwrote:
>>#include <stdio.h>
int main() {
int i;
for(i=1;i<=10;i ++)
main();
printf("C is urs..");
return 0;
}

[...]
>However a smart enough compiler could optimize it to:

#include <stdio.h>
int main(void) {
while (1) printf("C is urs..");
return 0;
}

(ignoring the failure to output any '\n's). The result should
progress considerably faster.

No, that would be an invalid optimization. In the original
program, even assuming infinite resources, the printf call will
never be reached.

It could be optimized to:

int main(void) {
while (1);
}
I looked again, and you are right. That should also be a faster
optimization, in terms of loops per unit time.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 18 '08 #14
On Jan 15, 12:09 pm, asit <lipu...@gmail. comwrote:
#include <stdio.h>

int main()
{
int i;
for(i=1;i<=10;i ++)
main();
printf("C is urs..");
return 0;

}

Why it shows error segmentation fault ??
I'll bite.

Like others were saying here the program you have written has infinite
recursion. Each time you call the main function it spawns ten more
copies of the main(), thus you will eventually consume all of your
machines memory or process. While technically you should not get a seg
fault it's actually a 'graceful' solution to this block of code.

When you write recursive algorithms you need to include a way for the
recursion to end.
The following code is one way to limit recursion.

void func(int limit)
{
int i;
for(i=1;i<=limi t;i++)
func(limit-1);

}

int main()
{
int i;
for(i=1;i<=10;i ++)
func(10);
printf("C is urs..");
return 0;
}

limit -1 is the key to end the recursion. The first instance of func()
call func() 10 times, the each of these call func()9 times all the way
down to 0 times. This example shows how to do recursion but, doesn't
do anything with it.

Hope this helps.
Jan 18 '08 #15

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

Similar topics

21
8309
by: user | last post by:
I just finish writing LAB2 with no errors when compiling, but once i run it, i get "segmentation fault".. i don't know what is wrong, can anyoen tell me what it means and how i can fix it? thx!
5
2998
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
18
26121
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)? Thanks.
27
3372
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
8
4931
by: lawrence k | last post by:
We made some changes to our server yesterday, and ever since, every single installation of WordPress that was on the server has stopped running. Other PHP scripts still run fine, but WordPress is dead. I logged into the server using ssh and looked the Apache error_log. The only thing there was a whole bunch of lines like this: child pid 27827 exit signal Segmentation fault (11)
3
5187
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
6
5045
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on gcc. The only difference is that in first I only call "main" and in second call
3
3190
by: weidongtom | last post by:
Hi, I was trying to implement a higher order function in C, summation over a range with a given function. And the summation function is then used to define sum_cubes. The program works fine when VAR is below 30000 or around that, but as soon as VAR is bigger, say 40000, or 50000, then I receive a segmentation error. Can anyone tell me why this is happening? Thanks in advance. #include <stdio.h>
0
1164
by: rmashukov | last post by:
Program terminated with signal 11, Segmentation fault. Environment: FreeBSD 6.2, Python 2.4.4, MySQLdb 1.2.2, SQLObject 0.9.1 I faced with an error in our multithreaded, network oriented application, wich interacts actively with MySQL database. Brief information from core dump is below (more detailed one is in the file attached). Though I am not very experienced with FreeBSD and gdb, I have still tried to collect information about the...
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8992
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.