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

c – 为什么返回后的语句会改变返回值?

发布时间:2020-12-16 03:38:06 所属栏目:百科 来源:网络整理
导读:C在以下代码中返回无效值: #include iostream#include vectorusing namespace std;int f(){ vector int * v[2]; return 1; v[1].push_back(NULL);}int main(){ cout f();} 输出是: 205960 当我在返回后通过线路时,它工作正常: #include iostream#include
C在以下代码中返回无效值:
#include <iostream>
#include <vector>

using namespace std;

int f(){
    vector< int * > v[2];
    return 1;
    v[1].push_back(NULL);
}

int main(){
    cout << f();
}

输出是:

205960

当我在返回后通过线路时,它工作正常:

#include <iostream>
#include <vector>

using namespace std;

int f(){
    vector< int * > v[2];
    return 1;
    //v[1].push_back(NULL);
}

int main(){
    cout << f();
}

输出是:

1

我使用mingw32-g .exe编译器的code :: blocks. mingw版本是:gcc版本4.4.1(TDM-2 mingw32).

解决方法

你的编译器有bug.幸运的是,它也已经过时了.你应该升级–G是4.6.2版本,它也实现了很多C 11,这非常有用.

如果你选择坚持使用较旧的编译器,那也是接受其缺陷的决定.

编辑:如果你真的坚持4.4(例如由于PHB),该系列仍然保持.您可以升级到今年4月发布的GCC 4.4.6.

(编辑:李大同)

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

    推荐文章
      热点阅读