Question:
Can you refer to the main function in C++?
ianoniymous
2010-10-13 19:07:41 UTC
Hey! I'm getting into C++, and every time I learn a new concept, I write a code just a bit more complex than the last one, to use that new concept. I just learned about Switches, shortly after learning about functions.

My question is:
Is it possible to refer to the main() function within a different function's code? I put, as one of the lines of code in a function, as follows:

/*blahblahblahblahblah*/
main();
/*blahblahblahblahblah*/

But my compiler returned an error on that line. Is it possible? And if so, what do you put as the code?
Four answers:
Cubbi
2010-10-14 03:43:08 UTC
NO. In C++, main() cannot be called from anywhere in your program, cannot be called recursively, and its address cannot be taken. Some compilers may allow this as a non-standard extension, but the language forbids it.



If your main() contains code that you want to execute more than once, it must be extracted into a standalone function (or executed as a loop, etc), but your suggested function call "main();" is an error.



to give a reference, ISO 14882:2003, paragraph 3.6.1/3

"The function main shall not be used within a program."

same document, paragraph C.1.2/3.6:

"Main cannot be called recursively and cannot have its address taken"
Shadow Wolf
2010-10-14 02:58:10 UTC
The language definition only allows for one main() function being the only required function in a C/C++ program. Offhand, I don't remember anything forbidding calls to main though.



Attempting to call the main() function from within the program could be a type of memory leak and lead to problems with error exits and other functions that force program termination. You would still have to call main() with all the required parameters. It could also be an odd way of building a recursive program if you got it to work and the compiler doesn't refuse to compile calls to main() once you get the correct parameters in the call and possibly a correct prototype to match. Expect program behavior to abnormal if you get it to work.



While it may be possible, there are no good reasons to make calls to main(). I suggest making an immediate call to MyMain() and creating what amounts to recursive calls that way. The C/C++ standards don't tell you that you have to do anything in main(). only that it has to exist. For example, Windows programs sometimes have to start with WinMain() in some compilers which tells us that main() is apparently used elsewhere.



Calling main() to "restart the program" is a very bad idea. Enclose your program in a loop and arrange for resetting and restarting your program inside the loop rather than call main(). You'll be creating a memory leak if you restart by calling main() because that will make your program recursive.



Short answer: Don't call main() as a means to restart the program until you learn a lot more about programming. By then you'll probably figure out why it is a really bad idea.



Shadow Wolf
2010-10-14 02:11:29 UTC
You bring up an interesting point.



After all these years i have never tried but thinking of it logically it should NOT be possible because what you are describing would create a sort of infinite loop since the main() function acts as the starting/ending point to a program and your calling it again from another function is like calling itself again to start/end. Granted, you could write special conditions that would prevent this infinite loop from happening (e.g. using a static variable in the main function) but still that is ugly.



So i think it would depend on the compiler you are using. I am willing to bet most compilers by default prevent this from happening seeing as how letting a programmer do this can lead to unexpected results.
j A L
2010-10-14 02:10:16 UTC
no, there can only be 1 main method in any program you write, they usually teach you not to even name anything else main.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...