为什么以及如何GCC编译缺少return语句的函数吗? [英] Why and how does GCC compile a function with a missing return statement?

查看:156
本文介绍了为什么以及如何GCC编译缺少return语句的函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>

char toUpper(char);

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

    return 0;
}

char toUpper(char c)
{
    if(c>='a'&&c<='z')
        c = c - 32;
}

在TOUPPER函数,返回类型为char,但在没有TOUPPER回归()。和编译源$ C ​​$ C与海湾合作委员会(GCC)4.5.1 20100924(红帽4.5.1-4)的Fedora-14。

In toUpper function, return type is char, but there is no "return" in toUpper(). And compile the source code with gcc (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4), fedora-14.

当然,发出警告:警告:控制​​到达非void函数结束,但是,做工精良

Of course, warning is issued: "warning: control reaches end of non-void function", but, working well.

什么在code发生编译用gcc时?
我想在这种情况下,一个坚实的答案。
感谢:)

What has happened in that code during compile with gcc? I want to get a solid answer in this case. Thanks :)

推荐答案

发生了什么事对你来说,当C程序被编译成汇编语言,你的toupper函数会弄成这个样子,也许是:

What happened for you is that when the C program was compiled into assembly language, your toUpper function ended up like this, perhaps:

_toUpper:
LFB4:
        pushq   %rbp
LCFI3:
        movq    %rsp, %rbp
LCFI4:
        movb    %dil, -4(%rbp)
        cmpb    $96, -4(%rbp)
        jle     L8
        cmpb    $122, -4(%rbp)
        jg      L8
        movzbl  -4(%rbp), %eax
        subl    $32, %eax
        movb    %al, -4(%rbp)
L8:
        leave
        ret

32的减法是在%eax寄存器进行。而在x86调用约定,就是在返回值预计将注册!所以...你真的很幸运。

The subtraction of 32 was carried out in the %eax register. And in the x86 calling convention, that is the register in which the return value is expected to be! So... you got lucky.

但请注意警告。他们是有原因的!

这篇关于为什么以及如何GCC编译缺少return语句的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆