Can a main function call itself in C ++?

Can someone tell me what is the problem with the code below?

int main () { return main(); } 

I tested, it compiles correctly. He works forever. A unique trick behind the scenes?

+14
c ++
Jan 24 '10 at 19:09
source share
9 answers

TL; DR . A call to main results in undefined behavior.




There seems to be a confusion about the terminology used in the standard and the implications for the programmer and compiler.

Firstly, the standard independently defines everything related to the C ++ language. If your particular version of a particular compiler allows for some specific action that has nothing to do with whether this action is legal. For the rest of the post, I mean the ISO03 standard.

So, to quote again, the standard reads in paragraph 3.3.1.3:

The main function should not be used inside the program.

In addition, in §3.2 it is “used” as:

An object or non-overloaded function is used if its name is displayed in a potentially evaluated expression.

This means that as soon as the program starts execution, main should never be entered again . This means that programmers cannot call main , this means that the compiler cannot insert another call to main (why it will, who knows), you cannot take the address of main and call, etc. You may not even have the potential to call main .

The only call to main should be made by the runtime library in which the program is running; all other calls cause undefined behavior. (This means that everything can happen!)




Now about the compiler behavior:

A diagnostic rule is defined as (§1.4.1):

The set of diagnosed rules consists of all syntactic and semantic rules in this International Standard, with the exception of those rules that explicitly indicate that “no diagnostics are required” or that are described as a result of “undefined behavior”.

In our case, in paragraph 3.3.1.3, a diagnostic rule is defined. Here are what compilers should do according to §1.4.2:

- If the program does not contain violations of the rules in this International Standard, the corresponding implementation should, within the limits of its resources, accept and correctly implement 3) this program.
- If the program contains a violation of any diagnosed rule, the corresponding implementation must issue at least one diagnostic message, except that - If the program contains a violation of the rule for which no diagnostics are required, this International Standard does not establish implementation requirements for this program.

Therefore, compilers are not required to apply the rules. All compilers must do is take well-formed programs (§1.3.14) and turn them into an executable program. The compiler is free to warn, mistakenly, etc., however, he likes it if it does not conflict with the language. In accordance with the second sentence, it is necessary to display a message in our particular case.

For this specific gcc task, the -pedantic parameter will warn of the illegality of calling main inside the program. Visual Studio will not warn about calling main , but at any warning level (greater than 0), it will warn about the recursive nature of the program.




What does all this mean in terms of the answers you should expect? This means that it is completely pointless to try to determine with certainty what the code fragment will publish. A call to main results in undefined behavior, and trying to define undefined behavior is obviously a lost cause. The only honest answer anyone can give is “what happens when I call main ?”? "Anything".

Hope this clarifies the situation.

+36
Jan 24 '10 at 20:43
source share

A call to main in C ++ is illegal (§3.6.1.3):

The main function should not be used inside the program.

Your compiler allows illegal behavior.

It is cyclical forever, because, main calls main , which calls main , which calls main , etc.

+24
Jan 24 '10 at 19:11
source share

He likes to be a drug dealer. Pretty illegal, but it compiles and even works well for some time ...

+8
Jan 24 '10 at 19:54
source share

The question is, why do you want?

main should be the only entry point for your program. Calling it again will significantly restart your program, but without a new process instance; no new stack, no new heap, etc.

If you really need recursion, call a separate function recursively.

+4
Jan 24 '10 at 19:12
source share

The C ++ standard in section 3.6.1 says:

The main function should not be used (3.2) inside the program.

It indicates that you should not call it from your program.

+4
Jan 24 '10 at 19:21
source share

Of course, if you really want to call your main function recursively, and sometimes there are good reasons, you should just do it

 int mymain() { return mymain(); } int main() { return mymain(); } 
+4
Jan 24 '10 at 22:27
source share

When you write recursive code, you need to make sure that at some point you stop recursing, otherwise you just wrote an infinite loop.

What do you have.

You should expect this code to go away for a long time, and then finally crash when the stack overflows.

+1
Jan 24 '10 at 19:14
source share

You have two questions. The first is a call to main, which, as already mentioned, violates the standard and the purpose of the standard.

The big problem is that you wrote a recursive call without any closing point. You doubt that you assume that the main version called by the original will just return. However, most languages ​​(in fact, everything that I can think of) allow unlimited recursion: if the function calls itself, then this version will also be. The only limitation is system resources.

So, you need to wrap the call in conditional terms and continue to call only when necessary. In your example, a global integer will be added, set to the number of levels that you want to process. Something like that:

`

 int levels = 3; int main() { if(levels) { cout << "Recursing another level...\n"; levels--; main(); } else { cout << "Reached the bottom.\n"; } return levels; } 

`

Coming out.

+1
Jan 24 '10 at 20:16
source share

Despite the fact that your program clearly does not make sense, since it is forever forever, it may be possible to do something like:

 int main( int argc, char* argv[] ) { if( argc ) { // do something with argv[0] main( argc - 1, &argv[1] ); } else { // rest of application having loaded your arguments } } 

However, the standard prohibits it. (See Other Answers)

Of course you can implement this by doing this

 int myFunc( int argc, char * argv[] ) { // I can recurse this if I like and just get main to forward straight to here } 
0
Sep 10 '14 at 9:45
source share



All Articles