Reputation: 4453
I developed many years in C and only now discovered that a program can execute code prior to main() function. Here is a code example
int generateNum(){
// Some malicious code here...
return 5;
}
static int someArray[] = {generateNum(),generateNum()}
int main(){
// Some code here...
}
The function generateNum()
is called twice before main()
.
My questions are
Upvotes: 7
Views: 410
Reputation: 1768
main
. This can be taken care of by the operating system loader/linker, or by some special module linked against the object file that contained main
. For gcc, this is described here: http://gcc.gnu.org/onlinedocs/gccint/Initialization.htmlUpvotes: 7
Reputation: 22821
A program shall contain a global function called main, which is the designated start of the program.
It doesn't say that no code executes before main
is called. Full quote:
3.6.1 Main function [basic.start.main]
1 A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [Note: in a freestanding environment, start-up and termination is implementation-defined; start-up contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. ]
Upvotes: 7