What happens if the main function recursively is called ???
int main() { main(); } What happens if the main function recursively is called ???
int main() { main(); } 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 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:
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)).inline , static or constexpr .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; .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).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.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
Source: https://ru.stackoverflow.com/questions/917026/
All Articles