Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not a valid C++ program. In fact, it crashes for me on Mac OSX after printing "Hello World".</p> <p>Disassembly shows <code>main</code> is a static variable, and there are initializers for it:</p> <pre><code>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) </code></pre> <p><strong>Why does it print "Hello World"?</strong></p> <p>The reason you see "Hello World" printed out is because it's run during static initialization of <code>main</code>, the static integer variable. Static initializers are called before C++ runtime even tries to call <code>main()</code>. When it does, it crashes, because <code>main</code> isn't a valid function, there is just an integer 195 in the data section of the executable.</p> <p>Other answers indicate this is a valid <code>ret</code> instruction and it runs fine in Linux, but it crashes on OSX, because the section is marked as non-executable by default.</p> <p><strong>Why can't a C++ compiler tell that main() isn't a function and stop with linker error?</strong></p> <p><code>main()</code> has C linkage, so the linker can't tell the difference between the type of the symbols. In our case, <code>_main</code> resides in the data section.</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload