473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

main() called inside main()

hi,

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

wat does the standard says about the above code snippet?

May 4 '06 #1
14 6809
Nothing,
This will result in infinite loop. Eating a lot of processor time, and
you may see your PC hang.

Thanks.

May 4 '06 #2
ra*********@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) ks***@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.
May 4 '06 #3
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
May 4 '06 #4
On 2006-05-04, ra*********@gmail.com <ra*********@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.
May 4 '06 #5
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.
May 4 '06 #6
Jordan Abel <ra*******@gmail.com> writes:
On 2006-05-04, ra*********@gmail.com <ra*********@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) ks***@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.
May 4 '06 #7
On 2006-05-04, Keith Thompson <ks***@mib.org> wrote:
Jordan Abel <ra*******@gmail.com> writes:
On 2006-05-04, ra*********@gmail.com <ra*********@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.
May 4 '06 #8
Jordan Abel wrote:
On 2006-05-04, Keith Thompson <ks***@mib.org> wrote:
Jordan Abel <ra*******@gmail.com> writes:
On 2006-05-04, ra*********@gmail.com <ra*********@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.
May 4 '06 #9
On 2006-05-04, Ian Collins <ia******@hotmail.com> wrote:
Jordan Abel wrote:
On 2006-05-04, Keith Thompson <ks***@mib.org> wrote:
Jordan Abel <ra*******@gmail.com> writes:

On 2006-05-04, ra*********@gmail.com <ra*********@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.

May 4 '06 #10
Jordan Abel wrote:
On 2006-05-04, Ian Collins <ia******@hotmail.com> wrote:
Jordan Abel wrote:
On 2006-05-04, Keith Thompson <ks***@mib.org> wrote:

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.

OK.
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.

How about dereferencing an odd address on a target the doesn't support this?

--
Ian Collins.
May 4 '06 #11
On 2006-05-04, Ian Collins <ia******@hotmail.com> wrote:
Jordan Abel wrote:
On 2006-05-04, Ian Collins <ia******@hotmail.com> wrote:
Jordan Abel wrote:

On 2006-05-04, Keith Thompson <ks***@mib.org> wrote:
>
>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.

OK.
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.

How about dereferencing an odd address on a target the doesn't support
this?


Things that would cause this to happen ALWAYS cause UB. UB doesn't
always mean something bad happens.
May 4 '06 #12

<ra*********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
hi,

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

wat does the standard says about the above code snippet?


This is allowed in C and performs the obvious useless recursive call. It is
forbidden however in the C++ standard
May 5 '06 #13
Robert Smith wrote:
<ra*********@gmail.com> wrote in message

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

wat does the standard says about the above code snippet?


This is allowed in C and performs the obvious useless recursive
call. It is forbidden however in the C++ standard


In C it is of primary use in writing obfuscated code. C++ needs no
assistance in this respect, and thus can dispense with the
capability.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
May 5 '06 #14
ra*********@gmail.com said:
hi,

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

wat does the standard says about the above code snippet?


Not a lot.

It's legal C, but eventually you'll run out of something or other, depending
on what system you are using.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 7 '06 #15

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

Similar topics

20
by: andre | last post by:
HI, I’m learning C# and already know VB .Net. I noticed that C# you have a Static Void Main () (entry point of the app). Well that got me thinking, I was told that VB.net removed “The...
16
by: Geoff Jones | last post by:
Hi What is the closest equivalent to Main in a VB.Net form? Geoff
3
by: googlinggoogler | last post by:
Hi, I would like to display a form (to add registration information) before the main form loads obviously I would like to place this inside an IF statement as I dont need to register every...
11
by: seattleboatguy | last post by:
I am trying to send a message from a visual c++ user-thread to the main window, so the main window can update text on the window to reflect what the user-thread is doing. I have tried 2 different...
1
by: thickface | last post by:
Hi all, I have a question and any suggestions for solutions would be greatly appreciated. In my program, I first defined a variable called 'incidence' which has to be updated at every time step, ...
7
by: Boki | last post by:
Hi All, When we delacre a main, why we dlacre it inside form? Why not outside of form? If form contains main(), why not every form contains a main()... Best regards, Boki.
12
by: onkar | last post by:
sometimes main accepts int main() and sometimes int main(int argc,char** argv) and sometimes int main(int argc,char **argv,char** env) still the code complies properly How is this possible ???...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
2
by: =?Utf-8?B?QS4gUm9iaW5zb24=?= | last post by:
Okay...this is my last post for the week!! I've been follwoing the examples in BOL/MSDN and yet I'm encountering issues constantly. This one is actually occuring when I'm in VS and trying to create...
25
by: mdh | last post by:
Hi all, Going quite methodically through K& R ( as some of you can attest to!), I have never seen a big diffference in declaring a function within "main" or "ahead" of it. Now, (p119, K&R II), the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.