相关疑难解决方法(0)

为什么在没有显式return语句的情况下,递归的返回调用会突破堆栈?

我看到了一个示例程序,用于演示递归,看起来它应该不起作用,但确实如此.逻辑很清楚,但为什么即使没有返回递归的函数调用它也能工作?return即使没有请求,看起来命令也会突破堆栈.这是语言标准还是gcc的东西?我在Windows和Linux上使用gcc编译的C和C++看到了它.

#include <iostream>
#include <cstdlib>

using namespace std;

int isprime(int num, int i)
{
   if (i == 1) {
      return 1;
   }
   else {
      if (num % i == 0)
         return 0;
      else
         isprime(num, i-1); // should be returned
   }
}


int main(int argc, char** argv)
{
   int input = atoi(argv[1]);
   cout << input << "\t" << isprime(input, input/2) << "\n";
}
Run Code Online (Sandbox Code Playgroud)

c++ recursion

18
推荐指数
2
解决办法
506
查看次数

函数返回std :: string崩溃没有return语句,不像返回int而没有return语句的函数

#include <iostream>
#include <string>
using namespace std;

string
crash()
{

}

int
noCrash()
{

}

int
main()
{
    crash(); // crashes
    // noCrash(); // doesn't crash
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

函数crash(),与Mingw g ++ 4.6.2崩溃,函数noCrash()执行没有问题.为什么在没有return语句的情况下函数返回字符串崩溃?

c++ crash return return-type return-value

3
推荐指数
4
解决办法
811
查看次数

c ++返回值

我在c ++中有以下代码:

    int fff ( int a , int b )
{
   if (a>b )
      return 0;
   else a+b ; 
}
Run Code Online (Sandbox Code Playgroud)

虽然我没有在'else'之后写'return'但它没有出错!在main()中我写的时候:

cout<<fff(1,2);
Run Code Online (Sandbox Code Playgroud)

它打印1?怎么会发生这种情况
可以解释一下吗?

c++ if-statement

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

比较字符串C++末尾的字符

该程序应该找到在Unix上输入的以".exe"结尾的命令行参数.由于某种原因,它不起作用.这是代码:

 int main( int argc, char* argv[] )
{
    for ( int i = 1; i < argc; i++)
    if( findExe( argv[i] ) )
      cout << argv[i] << endl;

  return 0;
}
bool findExe( char* argument )
{
  if ( strlen( argument ) >= 4 )
    {
      string testExe = ".exe";
      string initialWord=argument; //converts c-string to string
      string temp( initialWord,( initialWord.size() - 4 ),4 );//creates temp with last four characters from initialWord

      if ( !temp.compare(testExe) )
        return true;
    }
  else …
Run Code Online (Sandbox Code Playgroud)

c++ unix

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

标签 统计

c++ ×4

crash ×1

if-statement ×1

recursion ×1

return ×1

return-type ×1

return-value ×1

unix ×1