Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why calling main() is not allowed in C++

Tags:

c++

main

C++03 3.6.1.3: The function main shall not be used (3.2) within a program. ...

I wonder why this rule exists... Is anyone aware of any system/implementation where it would be a problem if main were used?

P.S. 1. I know the definition of the term used. 2. I know there are simple workarounds like calling a single MyMain() from main() and using MyMain() instead. 3. The question is about real-world implementations which would have a problem if the restriction weren't there. Thanks!

like image 936
Armen Tsirunyan Avatar asked Nov 10 '10 11:11

Armen Tsirunyan


People also ask

Can you call the main function in C?

In 'C' you can even call the main() function, which is also known as the "called function" of one program in another program, which is called "calling function"; by including the header file into the calling function.

Can a main function call another main function?

The main function always acts as a driver function and calls other functions. We can also write function call as a parameter to function.

How many times main function can be invoked in a program?

3) There is no limit on number of functions; A C program can have any number of functions.

Can you call int main in C++?

In C, C++ and few more programming languages, int main() is used as the entry point of the program. main() is the function from where the program will start to execute.


2 Answers

In addition to the other answers: The c++ spec guarantees that all static initialization has happened before main is called.

If code could call main then some static scoped object could call main, in which case a fundamental guarantee is violated.

The spec can't say "static scoped objects should not call main()" because many objects are not written specifically to be instantiated at static scope always. It also can't say that constructors should not call main() - because its very difficult to audit and prove that a constructor isn't calling a method, calling a method, that might sometimes, call main().

like image 182
Chris Becke Avatar answered Oct 18 '22 00:10

Chris Becke


I'd imagine this preserves an implementation's freedom to prefix main with code to construct globals and statics, accept any parameters identifying the environment and command line arguments and map them to the argc/argv/env conventions of C++, construct an appropriate stack and exception framework for the application to execute etc. Consider that not all environments may allow an executable image to have any other symbol designated as initialisation code to be run before main().

Similarly, cleanup code may be appended to main(), along with a call to the OS with some mapping from the 0/non-zero convention of C and C++ to the actual success/failure values used by that specific OS.

Consequently, calling main from elsewhere could attempt a second reinitialisation of the application framework or force an unintended exit to the OS - sounds catastrophic to me.

like image 34
Tony Delroy Avatar answered Oct 17 '22 22:10

Tony Delroy