Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the main() function re-entrant?

I heard that in C, main() is reentrant, while in C++ is not.

Is this true? What is the scenario of re-entering the main() function?

like image 309
Deqing Avatar asked Jun 27 '13 02:06

Deqing


1 Answers

Early C++ implementations, which were based on translation to C, implemented global constructors via adding a function call to the beginning of main. Under such an implementation, calling main again would re-run the global ctors, resulting in havoc, so it was simply forbidden to do so.

C on the other hand had no reason to forbid calling main, and it was always traditionally possible.

As for when it's useful, I would say "rarely". Most of the programs I've seen that called main were IOCCC entries.

like image 150
R.. GitHub STOP HELPING ICE Avatar answered Nov 07 '22 07:11

R.. GitHub STOP HELPING ICE