This is not a valid C ++ program. In fact, it crashes for me on Mac OSX after printing "Hello World".
The demo shows that main is a static variable, and there are initializers for it:
global constructors keyed to main: 0000000100000e20 pushq %rbp 0000000100000e21 movq %rsp,%rbp 0000000100000e24 movl $0x0000ffff,%esi 0000000100000e29 movl $0x00000001,%edi 0000000100000e2e leave 0000000100000e2f jmp __static_initialization_and_destruction_0(int, int)
Why is he typing "Hello World"?
The reason you see the "Hello World" printout is because it starts during the static initialization of main , a static integer variable. Static initializers are called before the C ++ environment even tries to call main() . When this happens, it will work, since main not a valid function, there is only an integer 195 in the data section of the executable file.
Other answers indicate that this is a valid ret statement, and it works fine on Linux, but it crashes on OSX because by default this section is flagged as non-executable.
Why can't the C ++ compiler say that main () is not a function and stops with a linker error?
main() has a C connection, so the linker cannot distinguish the type of characters. In our case, _main is in the data section.
start: 0000000100000eac pushq $0x00 0000000100000eae movq %rsp,%rbp ... 0000000100000c77 callq _main ; 1000010b0 0000000100000c7c movl %eax,%edi 0000000100000c7e callq 0x100000e16 ; symbol stub for: _exit 0000000100000c83 hlt ... ; the text section ends at 100000deb
Alex B Oct 05 '11 at 10:52 2011-10-05 10:52
source share