Why and how does GCC compile a function with a missing return statement?

后端 未结 8 1039
萌比男神i
萌比男神i 2020-12-01 23:57
#include 

char toUpper(char);

int main(void)
{
    char ch, ch2;
    printf(\"lowercase input : \");
    ch = getchar();
    ch2 = toUpper(ch);
             


        
8条回答
  •  半阙折子戏
    2020-12-02 00:48

    I can't tell you the specifics of your platform as I don't know it but there is a general answer to the behaviour you see.

    When the some function that has a return is compiled, the compiler will use a convention on how to return that data. It could be a machine register, or a defined memory location such as via a stack or whatever (though generally machine registers are used). The compiled code may also use that location (register or otherwise) while doing the work of the function.

    If the function doesn't return anything, then the compiler will not generate code that explicitly fills that location with a return value. However like I said above it may use that location during the function. When you write code that reads the return value (ch2 = toUpper(ch);), the compiler will write code that uses its convention on how retrieve that return from the conventional location. As far as the caller code is concerned it will just read that value from the location, even if nothing was written explicitly there. Hence you get a value.

    Now look at @Ray's example, the compiler used the EAX register, to store the results of the upper casing operation. It just so happens, this is probably the location that return values are written to. On the calling side ch2 is loaded with the value that's in EAX - hence a phantom return. This is only true of the x86 range of processors, as on other architectures the compiler may use a completely different scheme in deciding how the convention should be organised

    However good compilers will try optimise according to set of local conditions, knowledge of code, rules, and heuristics. So an important thing to note is that this is just luck that it works. The compiler could optimise and not do this or whatever - you should not reply on the behaviour.

提交回复
热议问题