473,772 Members | 2,420 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 #1
14 2669
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.
Jan 15 '08 #2
vi******@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.

--
Er*********@sun .com
Jan 15 '08 #3
vi******@gmail. com writes:
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.
The OP may or may not be a troll, but I wouldn't call the code
"perfectly valid". It's an infinite recursive loop, and the program
will attempt to consume infinite resources unless the compiler manages
to optimize away the recursion. main() calls itself 10 times. Each
of these 10 instances will again call main 10 times. And so on.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 15 '08 #4
asit wrote:
>
#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 ??
PLONK.

--
[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 16 '08 #5
Eric Sosman wrote:
vi******@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.
I'm not convinced that he's a troll, but I decided that whether he was a
troll or simply an unteachable idiot, he was worth adding to my
killfile.
Jan 16 '08 #6
Keith Thompson wrote:
vi******@gmail. com writes:
>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.

The OP may or may not be a troll, but I wouldn't call the code
"perfectly valid". It's an infinite recursive loop, and the program
will attempt to consume infinite resources unless the compiler manages
to optimize away the recursion. main() calls itself 10 times. Each
of these 10 instances will again call main 10 times. And so on.
It would be proper to say that main() calls itself once
or calls itself Aleph-nought times (depending on the point of
view), but I don't think "10 times" can be defended.

--
Er*********@sun .com
Jan 16 '08 #7
Eric Sosman <Er*********@su n.comwrites:
Keith Thompson wrote:
>vi******@gmail. com writes:
>>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.

The OP may or may not be a troll, but I wouldn't call the code
"perfectly valid". It's an infinite recursive loop, and the program
will attempt to consume infinite resources unless the compiler manages
to optimize away the recursion. main() calls itself 10 times. Each
of these 10 instances will again call main 10 times. And so on.

It would be proper to say that main() calls itself once
or calls itself Aleph-nought times (depending on the point of
view), but I don't think "10 times" can be defended.
Ok, main *in its initial invocation* attempts to call itself 10 times.
It fails to do so, because the first call will never return.

(Strictly speaking, main does call itself 10 times. It then calls
itself again and again and again until resources are exhausted. But
the value 10 is not related to the 10 in the for loop, so my statement
was correct only by coincidence.)

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 16 '08 #8
Keith Thompson wrote:
Eric Sosman <Er*********@su n.comwrites:
>Keith Thompson wrote:
>>vi******@gmail. com writes:
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.

The OP may or may not be a troll, but I wouldn't call the code
"perfectly valid". It's an infinite recursive loop, and the
program will attempt to consume infinite resources unless the
compiler manages to optimize away the recursion. main() calls
itself 10 times. Each of these 10 instances will again call
main 10 times. And so on.

It would be proper to say that main() calls itself once
or calls itself Aleph-nought times (depending on the point of
view), but I don't think "10 times" can be defended.

Ok, main *in its initial invocation* attempts to call itself 10
times. It fails to do so, because the first call will never return.

(Strictly speaking, main does call itself 10 times. It then calls
itself again and again and again until resources are exhausted.
But the value 10 is not related to the 10 in the for loop, so my
statement was correct only by coincidence.)
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.

--
[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 17 '08 #9
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);
}

The only difference in behavior would be the lack of memory exhaustion
(a "stack overflow" in some implementations ).

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 17 '08 #10

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

Similar topics

21
8307
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
3367
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
5043
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
9619
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
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
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
7460
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
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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.