Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   main() called inside main() (http://www.velocityreviews.com/forums/t442615-main-called-inside-main.html)

rahulsinner@gmail.com 05-04-2006 05:55 AM

main() called inside main()
 
hi,

int main(void)
{
main();
return 0;
}

wat does the standard says about the above code snippet?


Prasad 05-04-2006 06:01 AM

Re: main() called inside main()
 
Nothing,
This will result in infinite loop. Eating a lot of processor time, and
you may see your PC hang.

Thanks.


Keith Thompson 05-04-2006 08:10 AM

Re: main() called inside main()
 
rahulsinner@gmail.com writes:
> int main(void)
> {
> main();
> return 0;
> }
>
> wat does the standard says about the above code snippet?


It's a recursive call to main, which is perfectly legal.

Like any infinite recursive call, it will either exceed some system
resource limit, or it will run indefinitely (if the compiler optimizes
tail recursion).

The "return 0;" will never be reached.

--
Keith Thompson (The_Other_Keith) kst-u@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.

Flash Gordon 05-04-2006 11:11 AM

Re: main() called inside main()
 
Prasad wrote:
> Nothing,


Wrong, the standard says what will happen if you call main recursively.

> This will result in infinite loop. Eating a lot of processor time, and
> you may see your PC hang.


Or the compiler could fail to optimise it in which case the loop will
not be infinite since you will run out of stack space. Or process limits
might prevent it from eating a log of CPU time. Or the compiler might
optimise it to some form of sleep forever instruction.

> Thanks.


Please provide context when replying, Google is *not* Usenet. See the
Google section at http://clc-wiki.net/wiki/Intro_to_clc and the sites it
links to.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php

Jordan Abel 05-04-2006 12:27 PM

Re: main() called inside main()
 
On 2006-05-04, rahulsinner@gmail.com <rahulsinner@gmail.com> wrote:
> hi,
>
> int main(void)
> {
> main();
> return 0;
>}
>
> wat does the standard says about the above code snippet?


It may run forever, or crash. I don't know if a stack overflow (i.e.
running out of memory in a way that cannot be recovered from) is
considered undefined behavior.

Default User 05-04-2006 04:56 PM

Re: main() called inside main()
 
Prasad wrote:

> Nothing,
> This will result in infinite loop. Eating a lot of processor time, and
> you may see your PC hang.



See below.


Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.

Keith Thompson 05-04-2006 06:51 PM

Re: main() called inside main()
 
Jordan Abel <random832@gmail.com> writes:
> On 2006-05-04, rahulsinner@gmail.com <rahulsinner@gmail.com> wrote:
>> int main(void)
>> {
>> main();
>> return 0;
>>}
>>
>> wat does the standard says about the above code snippet?

>
> It may run forever, or crash. I don't know if a stack overflow (i.e.
> running out of memory in a way that cannot be recovered from) is
> considered undefined behavior.


I'm fairly sure it is, since the standard doesn't define what the
behavior is.

--
Keith Thompson (The_Other_Keith) kst-u@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.

Jordan Abel 05-04-2006 09:38 PM

Re: main() called inside main()
 
On 2006-05-04, Keith Thompson <kst-u@mib.org> wrote:
> Jordan Abel <random832@gmail.com> writes:
>> On 2006-05-04, rahulsinner@gmail.com <rahulsinner@gmail.com> wrote:
>>> int main(void)
>>> {
>>> main();
>>> return 0;
>>>}
>>>
>>> wat does the standard says about the above code snippet?

>>
>> It may run forever, or crash. I don't know if a stack overflow (i.e.
>> running out of memory in a way that cannot be recovered from) is
>> considered undefined behavior.

>
> I'm fairly sure it is, since the standard doesn't define what the
> behavior is.


I guess i'm used to thinking of UB as something that can at least in
principle be identified by careful examination of the source. that is,
for any given set of inputs ["inputs" including returns from library
functions whose outputs are not fully determined by their inputs - e.g.
malloc returning null or not], a program either does cause UB or
doesn't. stack overflows are a big hole in this, and i think they're
unique.

Ian Collins 05-04-2006 09:45 PM

Re: main() called inside main()
 
Jordan Abel wrote:
> On 2006-05-04, Keith Thompson <kst-u@mib.org> wrote:
>
>>Jordan Abel <random832@gmail.com> writes:
>>
>>>On 2006-05-04, rahulsinner@gmail.com <rahulsinner@gmail.com> wrote:
>>>
>>>>int main(void)
>>>>{
>>>> main();
>>>> return 0;
>>>>}
>>>>
>>>>wat does the standard says about the above code snippet?
>>>
>>>It may run forever, or crash. I don't know if a stack overflow (i.e.
>>>running out of memory in a way that cannot be recovered from) is
>>>considered undefined behavior.

>>
>>I'm fairly sure it is, since the standard doesn't define what the
>>behavior is.

>
>
> I guess i'm used to thinking of UB as something that can at least in
> principle be identified by careful examination of the source. that is,
> for any given set of inputs ["inputs" including returns from library
> functions whose outputs are not fully determined by their inputs - e.g.
> malloc returning null or not], a program either does cause UB or
> doesn't. stack overflows are a big hole in this, and i think they're
> unique.


Are they UB and are they unique?

I'd categorise them as an environment constraint violation, another
example would be opening more files then the operating environment permits.

Stack size is environment specific, so a well formed program that
operates correctly in one environment might violate the constraints of
another.

--
Ian Collins.

Jordan Abel 05-04-2006 09:56 PM

Re: main() called inside main()
 
On 2006-05-04, Ian Collins <ian-news@hotmail.com> wrote:
> Jordan Abel wrote:
>> On 2006-05-04, Keith Thompson <kst-u@mib.org> wrote:
>>
>>>Jordan Abel <random832@gmail.com> writes:
>>>
>>>>On 2006-05-04, rahulsinner@gmail.com <rahulsinner@gmail.com> wrote:
>>>>
>>>>>int main(void)
>>>>>{
>>>>> main();
>>>>> return 0;
>>>>>}
>>>>>
>>>>>wat does the standard says about the above code snippet?
>>>>
>>>>It may run forever, or crash. I don't know if a stack overflow (i.e.
>>>>running out of memory in a way that cannot be recovered from) is
>>>>considered undefined behavior.
>>>
>>>I'm fairly sure it is, since the standard doesn't define what the
>>>behavior is.

>>
>>
>> I guess i'm used to thinking of UB as something that can at least in
>> principle be identified by careful examination of the source. that is,
>> for any given set of inputs ["inputs" including returns from library
>> functions whose outputs are not fully determined by their inputs - e.g.
>> malloc returning null or not], a program either does cause UB or
>> doesn't. stack overflows are a big hole in this, and i think they're
>> unique.

>
> Are they UB and are they unique?
>
> I'd categorise them as an environment constraint violation, another
> example would be opening more files then the operating environment permits.


That doesn't cause UB. It causes fopen to return a null pointer.

> Stack size is environment specific, so a well formed program that
> operates correctly in one environment might violate the constraints of
> another.


Which is unique among ALL things which cause UB.



All times are GMT. The time now is 05:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.