A friend of mine in his first programming class at Uni asked what return did and why we return 0. I explained to him what it does and why that's usually the case. He then asked me what would happen if this was the return statement
return (int)((2^(sizeof(int)*8)/2)+1);
I'm assuming he wanted to return signed overflow to the OS, but I have no idea what would happen. I'm sure modern operating systems have some built in to keep this from doing anything bad, heck, I'm sure operating systems have had something like that for over a decade, but assuming they didn't and assuming the program compiles without error, what would happen when the program exits?
It would be evaluated before it is returned. Although since signed int overflow is undefined in C++ IIRC, it could do anything. (i.e. the compiler could generate whatever code it wants).
I can count at least three things wrong with that expression, including undefined behavior, but I guess that's supposed to be INT_MAX+1 written in a very retarded way.
The OS doesn't use the return value from a program, it merely returns it to the calling program (such as a console script) so it can use it or ignore it. The question of what happens when a program returns some value is like asking what happens when an int function returns a value. Nothing happens. The value is given meaning only by how it is interpreted by the caller.
@helios, Like I said, he's in his first programming class. He knew what he was trying to do, just not how to do it :P
As for the second part, this is all hypothetical. Suppose it's called by the kernel, or using an interface to the kernel. I guess I was assuming that the Kernel would try to handle it in some way and it would through an error of some kind, but either way it was a pretty stupid train of thought xD. After reading your posts I realized how obvious the answer should have been.
I do not see how this would be any issue. For one that return value isn't anywhere near the max int value (even if he was actually using an exponential function). And even if it was it would overflow an int and crash before returning anything. Unless Im missing something.