Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > main() called inside main()

Reply
Thread Tools

main() called inside main()

 
 
rahulsinner@gmail.com
Guest
Posts: n/a
 
      05-04-2006
hi,

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

wat does the standard says about the above code snippet?

 
Reply With Quote
 
 
 
 
Prasad
Guest
Posts: n/a
 
      05-04-2006
Nothing,
This will result in infinite loop. Eating a lot of processor time, and
you may see your PC hang.

Thanks.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-04-2006
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
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      05-04-2006
On 2006-05-04, (E-Mail Removed) <(E-Mail Removed)> 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.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      05-04-2006
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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-04-2006
Jordan Abel <(E-Mail Removed)> writes:
> On 2006-05-04, (E-Mail Removed) <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      05-04-2006
On 2006-05-04, Keith Thompson <(E-Mail Removed)> wrote:
> Jordan Abel <(E-Mail Removed)> writes:
>> On 2006-05-04, (E-Mail Removed) <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-04-2006
Jordan Abel wrote:
> On 2006-05-04, Keith Thompson <(E-Mail Removed)> wrote:
>
>>Jordan Abel <(E-Mail Removed)> writes:
>>
>>>On 2006-05-04, (E-Mail Removed) <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      05-04-2006
On 2006-05-04, Ian Collins <(E-Mail Removed)> wrote:
> Jordan Abel wrote:
>> On 2006-05-04, Keith Thompson <(E-Mail Removed)> wrote:
>>
>>>Jordan Abel <(E-Mail Removed)> writes:
>>>
>>>>On 2006-05-04, (E-Mail Removed) <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 2 05-20-2008 02:20 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
Why is a button Click event also called when a textbox TextChanged event is called??? S_K ASP .Net 6 11-08-2007 07:44 PM
What is called carry chain structure in FPGA is called in IC? Weng Tianxiang VHDL 6 09-12-2007 07:19 AM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments