Connecting Tech Pros Worldwide Forums | Help | Site Map

segmentation fault in recursion

asit
Guest
 
Posts: n/a
#1: Jan 15 '08
#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 ??

vippstar@gmail.com
Guest
 
Posts: n/a
#2: Jan 15 '08

re: segmentation fault in recursion


On Jan 15, 10:09 pm, asit <lipu...@gmail.comwrote:
Quote:
#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.
Eric Sosman
Guest
 
Posts: n/a
#3: Jan 15 '08

re: segmentation fault in recursion


vippstar@gmail.com wrote:
Quote:
On Jan 15, 10:09 pm, asit <lipu...@gmail.comwrote:
Quote:
>#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.
Quote:
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.

--
Eric.Sosman@sun.com
Keith Thompson
Guest
 
Posts: n/a
#4: Jan 15 '08

re: segmentation fault in recursion


vippstar@gmail.com writes:
Quote:
On Jan 15, 10:09 pm, asit <lipu...@gmail.comwrote:
Quote:
>#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_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
CBFalconer
Guest
 
Posts: n/a
#5: Jan 16 '08

re: segmentation fault in recursion


asit wrote:
Quote:
>
#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

Mark Bluemel
Guest
 
Posts: n/a
#6: Jan 16 '08

re: segmentation fault in recursion


Eric Sosman wrote:
Quote:
vippstar@gmail.com wrote:
Quote:
>On Jan 15, 10:09 pm, asit <lipu...@gmail.comwrote:
Quote:
>>#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.
Eric Sosman
Guest
 
Posts: n/a
#7: Jan 16 '08

re: segmentation fault in recursion


Keith Thompson wrote:
Quote:
vippstar@gmail.com writes:
Quote:
>On Jan 15, 10:09 pm, asit <lipu...@gmail.comwrote:
Quote:
>>#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.

--
Eric.Sosman@sun.com
Keith Thompson
Guest
 
Posts: n/a
#8: Jan 16 '08

re: segmentation fault in recursion


Eric Sosman <Eric.Sosman@sun.comwrites:
Quote:
Keith Thompson wrote:
Quote:
>vippstar@gmail.com writes:
Quote:
>>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_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
CBFalconer
Guest
 
Posts: n/a
#9: Jan 17 '08

re: segmentation fault in recursion


Keith Thompson wrote:
Quote:
Eric Sosman <Eric.Sosman@sun.comwrites:
Quote:
>Keith Thompson wrote:
Quote:
>>vippstar@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

Keith Thompson
Guest
 
Posts: n/a
#10: Jan 17 '08

re: segmentation fault in recursion


CBFalconer <cbfalconer@yahoo.comwrites:
[...]
Quote:
Quote:
Quote:
>>>>asit <lipu...@gmail.comwrote:
>>>>>
>>>>>#include <stdio.h>
>>>>>>
>>>>>int main() {
>>>>> int i;
>>>>> for(i=1;i<=10;i++)
>>>>> main();
>>>>> printf("C is urs..");
>>>>> return 0;
>>>>>}
[...]
Quote:
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_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Willem
Guest
 
Posts: n/a
#11: Jan 17 '08

re: segmentation fault in recursion


Keith wrote:
) CBFalconer <cbfalconer@yahoo.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
user923005
Guest
 
Posts: n/a
#12: Jan 17 '08

re: segmentation fault in recursion


On Jan 15, 1:49*pm, Eric Sosman <Eric.Sos...@sun.comwrote:
Quote:
vipps...@gmail.com wrote:
Quote:
On Jan 15, 10:09 pm, asit <lipu...@gmail.comwrote:
Quote:
#include <stdio.h>
>
Quote:
Quote:
int main()
{
* * * * int i;
* * * * for(i=1;i<=10;i++)
* * * * * * * * main();
* * * * printf("C is urs..");
* * * * return 0;
>
Quote:
Quote:
}
>
Quote:
Quote:
Why it shows error segmentation fault ??
>
Quote:
Because you're a troll.
>
* * *You may be right about that.
>
Quote:
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.
Army1987
Guest
 
Posts: n/a
#13: Jan 18 '08

re: segmentation fault in recursion


vippstar wrote:
Quote:
On Jan 15, 10:09 pm, asit <lipu...@gmail.comwrote:
Quote:
>#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")
CBFalconer
Guest
 
Posts: n/a
#14: Jan 18 '08

re: segmentation fault in recursion


Keith Thompson wrote:
Quote:
CBFalconer <cbfalconer@yahoo.comwrites:
[...]
Quote:
>asit <lipu...@gmail.comwrote:
>>
Quote:
>>#include <stdio.h>
>>int main() {
>> int i;
>> for(i=1;i<=10;i++)
>> main();
>> printf("C is urs..");
>> return 0;
>>}
>
[...]
>
Quote:
>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

JosephSlomka@gmail.com
Guest
 
Posts: n/a
#15: Jan 18 '08

re: segmentation fault in recursion


On Jan 15, 12:09 pm, asit <lipu...@gmail.comwrote:
Quote:
#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<=limit;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.
Closed Thread