What happens when we declare int main() ?

So what i understood was, by declaring int main() at the start of the program, the compiler instructs the OS to allocate a memory space which is equal to the size of 'int' apart from the variables that are going to be specified, for that particular block of the program.....is that right? If no, please explain as much as possible...thanx for having a look :)
closed account (zb0S216C)
The int, main()'s parameters, return value and return/start address are all stored on the program's stack. The OS will invoke main() from the starting address and will go from there.

I can't see main() being stored anywhere else; it wouldn't make much sense.

Wazzak
Hi pragu,

This code says that main is returning an int to the OS.

int main()
It is my understanding that the return value as well as all arguments and non-static variables declared inside the function are pushed onto the thread stack before the function is executed and popped off the stack when the function returns.

When the OS loads your program it doesn't immediately execute your main function. Instead it calls a startup function inside the C/C++ runtime library that initializes the heap and calls constructors for all class objects. Once the runtime is initialized it calls your program's main function. When your program returns from main() to the runtime startup function it calls destructors for all class objects and frees the heap. Eventually the startup function ends your program's process using the exit code returned from main().
the compiler instructs the OS to allocate a memory space which is equal to the size of 'int'


I was pretty sure that the OS would allocate more than 4 bytes to a process.
From: http://en.wikipedia.org/wiki/Main_function_%28programming%29#C_and_C.2B.2B

In C++ (unlike C) main() cannot be called recursively


This implies main COULD call itself in C? Sounds scary :O
Topic archived. No new replies allowed.