Bloomberg LP Interview Question
Software Engineer / Developersreturn main;
Would need to be casted to return the address of the function main.
If it was:
return main();
It should call the main function again and again until the stack overflows. Each time main() is called, it pushes the program counter onto the stack. I don't know the size of the address it pushes, but I don't think any other variables are pushes on the stack after each call. I could be wrong though.
Main cannot call itself and the compiler will issue an error if you call main within main. Main is an entry point to the program, not a user-defined func.
vatson.. Where did you read that it is not legal in C++? I might be wrong, but I believe it is legal in C++ as well. The operating system might call all sorts of stuff before it gets to main, but I believe main is just a regular function.
CG... if the code was:
int main(){
return main;
}
It would not compile as it is misleading what main is. If you cast main to an int, it would return the address of the main function as an int.
int main(){
return (int)main;
}
would return something like 4427357.
It the code was:
int main(){
return main();
}
It would recursively keep calling main(). The stack would eventually overflow because each time we call main(), we push the program counter onto the stack.
Some psuedo assembly to illustrate the point.
0000: NOP // main function.. NOP = no operation
0001: Mov 40, 01h; // Some random instruction (meaningless)
0003: Call 0000h; // call the main subroutine again
0007: RTS // End the subroutine (we never get here)
Each time we get to the position 0003 we call the function at 0000. You need to know were you will be after the function finishes, so right before you call the function, you push the address 0007 on the stack. Since each call to the function at 0000 will call another function, you never get to return and the stack will continue to grow with all those 0007's you keep pushing.
True we might not use it...but the idea behind such questions is usually the conceptual understanding. It gives a clear idea whether you know what goes on behind the scenes and how compilers work.
Just to add ...Chronotrigger....it is illegeal in C++ and is categorized as undefined behavior. So even though you got it compiled successfully, you never know how its going to act.
g++:
g++ test.cpp -ansi -o test.o
test.cpp: In function `int main()':
test.cpp:6: error: invalid conversion from `int (*)()' to `int'
make: *** [all] Error 1
gcc:
gcc test.c -ansi -o test.o
test.c: In function `main':
test.c:3: warning: return makes integer from pointer without a cast
test.o
make: *** [all] Error 52
I hear lot of answers here..
But the correct answer is it is gonna compile fine and when you run this one it will run for a few seconds until the stack runs out of memory. Each time you call main, it should have the return address which is 4 bytes in the stack. So eventually its gonna eat all the 4 bytes that the OS provides and once it runs out of the memory it is gonna give you segmentation fault because it is illegally using some address for the next function call once it runs out of memory.
To see interesting things you could print a message saying
main()
{
printf("I am in the main \n");
main();
}
This will give you how exactly the first few seconds are gonna run by printing the same sentence again and again.
Good question although not that tough.
Hope this helps.
They asked my this questions also, Any answer? Please....
- Dong February 07, 2006