Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this program keep crashing

Tags:

c++

visual-c++

It crashes on execution:

#include <iostream>

int main ()

{
    if(main());
    return 0;
}

Why?

like image 890
user720967 Avatar asked Apr 22 '11 19:04

user720967


2 Answers

It crashes due to Stackoverflow of course, since, there is no terminating condition, but technically the C++ Compiler is allowed not to compile it, since in C++:

main() cannot be called from within a program.
The address of main() cannot be taken.
The main() function cannot be overloaded.

What the standard says:

Annex to C Compatibilty

3.6

Change: Main cannot be called recursively and cannot have its address taken
Rationale: The main function may require special actions.
Effect on original feature: Deletion of semantically well-defined feature
Difficulty of converting: Trivial: create an intermediary function such as mymain(argc, argv).
How widely used: Seldom
like image 186
Sadique Avatar answered Nov 06 '22 20:11

Sadique


ISO/IEC 14882:2003(E) 3.6.1 (3)

The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementation-defined. A program that declares main to be inline or static is ill-formed. The name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. ]

like image 40
Mahesh Avatar answered Nov 06 '22 21:11

Mahesh