加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 缺少返回值的函数,运行时的行为

发布时间:2020-12-16 03:14:48 所属栏目:百科 来源:网络整理
导读:如预期的那样,编译器(VisualStudio 2008)将发出警告 warning C4715: ‘doSomethingWith’ : not all control paths return a value 编译以下代码时: int doSomethingWith(int value){ int returnValue = 3; bool condition = false; if(condition) // retur
如预期的那样,编译器(VisualStudio 2008)将发出警告

warning C4715: ‘doSomethingWith’ : not
all control paths return a value

编译以下代码时:

int doSomethingWith(int value)
{
    int returnValue = 3;
    bool condition = false;

    if(condition)
        // returnValue += value; // DOH

    return returnValue;
}

int main(int argc,char* argv[])
{
    int foo = 10;
    int result = doSomethingWith(foo);
    return 0;
}

但程序运行正常.函数doSomethingWith()的返回值为0.

是只是未定义的行为,还是有一定的规则如何在运行时创建/计算结果值.非POD数据类型作为返回值会发生什么?

解决方法

它是ISO C标准第6.6.3节中规定的未定义行为:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读