相关疑难解决方法(0)

在c ++和优化中缺少返回的不稳定行为

假设你用c ++编写了一个函数,但是心不在焉地忘记输入这个单词return.在那种情况下会发生什么?我希望编译器会抱怨,或者一旦程序达到这一点,至少会出现一个分段错误.然而,实际发生的事情要糟糕得多:程序会喷出垃圾.不仅如此,实际输出还取决于优化水平!以下是一些演示此问题的代码:

#include <iostream>
#include <vector>

using namespace std;

double max_1(double n1,
         double n2)
{
  if(n1>n2)
    n1;
  else
    n2;
}

int max_2(const int n1,
      const int n2)
{
  if(n1>n2)
    n1;
  else
    n2;
}

size_t max_length(const vector<int>& v1,
          const vector<int>& v2)
{
  if(v1.size()>v2.size())
    v1.size();
  else
    v2.size();
}

int main(void)
{
  cout << max_1(3,4) << endl;
  cout << max_1(4,3) << endl;

  cout << max_2(3,4) << endl;
  cout << max_2(4,3) << endl;

  cout << max_length(vector<int>(3,1),vector<int>(4,1)) << endl;
  cout << max_length(vector<int>(4,1),vector<int>(3,1)) << …
Run Code Online (Sandbox Code Playgroud)

c++ return vector undefined-behavior

2
推荐指数
1
解决办法
177
查看次数

当函数在结尾处缺少return语句时,为什么它可以工作?

我正在学习C++.下面的代码让我困惑:

int test_return(int a)
{
    for (int i = 40; i < 44; i++)
    {
        if (i == a)
        {
            cout << "return here with i: " << i << endl;
            return 59;
        }
    }
}

int main()
{
    cout << "in main: " << test_return(61) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道我在函数结束时错过了一个return语句test_return.

但编译器说没有错误,它在执行时有效.

于是,我拿起一些特殊号码,如40,44,59,61,看看哪一个函数test_return会选择回国.

我试过好几次,输出总是如下:

in main: 44
Run Code Online (Sandbox Code Playgroud)

似乎函数在for语句结束之前test_return返回了int i.

我的问题是:

这合法吗?

它是如何工作的?

更新:

我在函数末尾添加了这些代码 …

c++ return function

2
推荐指数
1
解决办法
568
查看次数

Gcc 4.8.3没有发现缺少'return'关键字

我们来看看这段代码:

#include <iostream>

int foo(int i) {return i; }

int foobar(int z) {return foo(z);}

int main() {
std::cout << foobar(3) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

它用g ++ -std = c ++ 11编译好...并给出输出3.但是相同的输出由下式给出:

#include <iostream>

int foo(int i) {return i; }

int foobar(int z) { foo(z);}

int main() {
std::cout << foobar(3) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

它编译没有问题,但显然foobar错过了关键字返回.这是gcc 4.8.3中的错误还是我不知道某些c ++ 11原则?(在Fedora 20上运行)

c++ gcc c++11 gcc4.8

1
推荐指数
1
解决办法
272
查看次数

clang ++ bug还是我不明白的东西?

我用g ++编译了以下代码并且它有效.

bool keyExists(Obj key){
    findIn(key,true,false,false,nullptr,nullptr,1,0,0);
}
Run Code Online (Sandbox Code Playgroud)

我使用clang ++编译它,当程序运行时它冻结了.

我把线改为:

bool keyExists(Obj key){
    return findIn(key,true,false,false,nullptr,nullptr,1,0,0);
    //findIn(key,true,false,false,nullptr,nullptr,1,0,0);
}
Run Code Online (Sandbox Code Playgroud)

现在它的工作原理.

我想它不应该那样工作.这是Clang的已知错误还是一个特例?

c++ clang

0
推荐指数
1
解决办法
70
查看次数

当复制赋值函数没有返回任何内容时,为什么编译器不会引发错误?

我已经在VS2013和Dev-C++上运行了这个代码但是当副本赋值没有返回任何实际应该的时候,编译器不会引发任何错误,请帮我解释一下.

#include <iostream>
using namespace std;
class sample
{
public:
    sample()
    {
        cout << "X::X()" << endl;
    }
    sample(sample const &)
    {
        cout << "X::X( X const & )" << endl;
    }
    sample& operator=(sample const &)
    {
        cout << "X::operator=(X const &)" << endl;
    }
};
sample f()
{
    sample tmp;
    return tmp;
}
int main()
{
    int a;
    sample x = f();
    cin >> a;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我改为:

sample x;
x = f();
Run Code Online (Sandbox Code Playgroud)

VS2013编译器会引发如下错误:错误1错误C4716:'sample :: operator =':必须返回值c:\ users\xxx\desktop\test\test\main.cpp …

c++

0
推荐指数
1
解决办法
65
查看次数

标签 统计

c++ ×5

return ×2

c++11 ×1

clang ×1

function ×1

gcc ×1

gcc4.8 ×1

undefined-behavior ×1

vector ×1