Can main function call itself in C++? -


Can anyone say that what is the problem of the code given below?

  int main () {return home (); }  

I tested, is it compiled correctly, is it always going to move again behind the scene?

TLR : undefined in calling main result Behavior


Illusion about the terminology used in the standard, and the implications for programmers and compilers, it seems.

First of all, the standard alone sets everything about the C ++ language if a specific action is allowed from your particular edition of a particular compiler, its There is no effect whether this work is legal or not. For the rest of the post, I am referring to the ISO03 standard.

Then again for the quote, the standard §3.6.1.3 says:

The function will not be used primarily within a program. In addition, §3.2 defines that as an "experiment":

An object or non-overloaded function is used to name it Appears in a potentially assessed expression.

This means that once the program starts, main should never be registered again . This means that programmers can not do main to call, that means why the compiler can not place another call on the main (why this happens, who is ), You can not find the main address and call, etc. You can not even get the ability to call main .

The main should only be running from the library while the program is running; All other calls apply undefined behavior. (Which means that anything can happen!)


Now on the compiler behavior:

defined as a diagnostic rule (§1.4.1) :

The set of diagnostic rules consists of all syntax and economic rules in this international standard, except those rules that "no diagnostic is required" or "undefined behavior "As a result.

In our case, §3.6.1.3 defines a clinical rule §1.4.2:

According to the compiler what to do - If there is no violation of the rules in this international standard in any program, then a custom implementation, in its processing limits, accept and execute correctly Execute 3) that program.
- If any program violates any diagnostic rule, then an analog implementation will issue at least one diagnostic message, except that - - If a program violates a rule for which There is no diagnosis required, this international standard does not require any implementation on the program.

Therefore, the compiler not is required by all the compilers to implement the well-built program (§1.3. 14) and convert them into executable programs. A compiler is free for warning, error, etc. Although it does not like it, unless it struggles with language.

For this particular problem, the GCC has required to display a message in its special case on the -fantic = / >> option The main will warn about the invalidity of calling the Visual Studio will not be warned about calling main , but at the level of any warning (more than 0) of this program Warning about recursive nature.


What does all of this answer to be expected of you? This means that it is completely meaningless to make sure that the code snippets will post. Trying to define calling main results in undefined behavior, and undefined behavior is clearly a lost cause, only honest answers call "when i call main What happens then? " Is "anything."

I hope this clears things up.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -