What happens if the main function recursively is called ???

int main() { main(); } 
  • one
    This is an ill-formed program, i.e. compilation error. - AnT
  • one
    @trademark: The diagnostic message required by the language standard from the compiler did you receive? This is a compilation error. - AnT
  • one
    @trademark: The term "compiles" in the C ++ world means "is being compiled without issuing the diagnostic messages required by the standard language." If you have received the required diagnostic message, it is called NOT compiled . If you did not receive the required diagnostic message for this code, then this is a compiler bug. From the point of view of the C ++ language, the code is erroneous. Everything else - features / glitches / bugs of your compiler - AnT
  • one
    @trademark: Your "teacher" is nonsense. C and C ++ compilers work on the principle of "garbage at the entrance - garbage at the output." If any garbage at the entrance is “compiled and launched”, then congratulations, the flag is in your hands, but it has nothing to do with the C ++ language. - AnT

4 answers 4

The standard prohibits the use of the function main

6.6.1 main function
3. The function is not used within a program. ...

However, some compilers may deviate from standard behavior.

For example, take clang 3.8.0 .
With the -pedantic-errors flag, the compiler gives the error:

error: ISO C ++ doesn’t allow

https://rextester.com/QBKCG46413

However, if you remove this option, the behavior may be unexpected. Take a small code:

 #include <iostream> struct Some { Some() { std::cout << "begin" << std::endl; } ~Some() { std::cout << "end" << std::endl; } }; Some s; int main() { main(); } 

The object s designed simply to display messages before main and after its execution.

If we compile it with the -O0 flag, then we get an honest SIGSEGV : https://rextester.com/XTRATW71243

However, already with the -O1 flag we see the output of both messages and no errors.

begin
end

those. main successful: https://rextester.com/XUF23687

This is due to optimizations for undefined behavior. The fact is that infinite recursion without a change in state is an indefinite behavior, so the compiler has the right to do anything. In this case, clang sees that recursion does not change the external state, and there is only one exit point - return 0 at the end of main (it will be added by the compiler automatically, because there is no explicit return in main ), so all this recursion can be legitimately removed:

4.6 Program execution
1. Define a parameterized nondeterministic abstract machine. Conforming implementations. In particular, they need to copy the structure of the abstract machine. Rather conforming implementations are described below. 6
...
6) This rule is sometimes referred to as the "as-if" rule. observable behavior of the program. For example, it’s not a problem.

If you look at the code of the main function after compilation, it is as follows:

 main: # @main xor eax, eax ret 

https://godbolt.org/z/63h1rn

    The language standard prohibits any use of the main function:

    6.6.1 main function [basic.start.main]
    3 The function is not used within a program.

      Stack overflow because there is no condition for completing recursion ...

      By the way, VC ++ 2017, for example, honestly warns about this. But GCC on ideone compiles without warning.

        The main function has the following special properties:

        1. It can not be used anywhere in the program,
          • in particular, it cannot be called recursively;
          • you can not take her address.
        2. It cannot be declared and cannot be overloaded: in fact, the name main reserved in the global namespace for functions (although this name can be used to name classes, namespaces, enumerations, and any entities not in global namespaces, except that the function named 'main' cannot be declared with binding for the C language in any namespace (starting with C ++ 17)).
        3. It cannot be declared as deleted or defined with a binding for C (starting with C ++ 17), inline , static or constexpr .
        4. In the body of the main function, the return optional: at the completion of the main function without the return operator, the effect will be the same as at return 0; .
        5. Executing a return (or implicit return when the end of the main function is reached) is equivalent to a normal exit from the function (which destroys objects with an automatic lifetime) and then calling std::exit with the same argument that was passed to return . ( std::exit destroys static objects and terminates the program).
        6. If the main function is defined as function-try-block , exceptions thrown by the destructors of static objects (which are destroyed when std::exit called) are not caught by the function.
        7. (since C ++ 14) The return type of the function by the main function cannot be inferred ( auto main() {...} not allowed).

        https://ru.cppreference.com/w/cpp/language/main_function https://en.cppreference.com/w/cpp/language/main_function