Why does declaring main as an array compile?

前端 未结 6 1998
一整个雨季
一整个雨季 2021-01-31 07:35

I saw a snippet of code on CodeGolf that\'s intended as a compiler bomb, where main is declared as a huge array. I tried the following (non-bomb) version:



        
6条回答
  •  误落风尘
    2021-01-31 07:47

    main is - after compiling - just another symbol in an object file like many others (global functions, global variables, etc).

    The linker will link the symbol main regardless of its type. Indeed, the linker cannot see the type of the symbol at all (he can see, that it isn't in the .text-section however, but he doesn't care ;))

    Using gcc, the standard entry point is _start, which in turn calls main() after preparing the runtime environment. So it will jump to the address of the integer array, which usually will result in a bad instruction, segfault or some other bad behaviour.

    This all of course has nothing to do with the C-standard.

提交回复
热议问题