【问题标题】:Recursive function showing segmentation issue C++递归函数显示分段问题 C++
【发布时间】:2020-11-22 12:08:21
【问题描述】:

此代码用于递归函数练习。当我运行代码时,它停在“POWER” cout 行,然后我的编译器显示分段错误。 POWER 行后面的函数应该递归地将数字“a”提升到数字“b”的幂。我不知道如何解决这个问题,有人可以帮忙吗?

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

/**** Recursive backwards print, prints a string starting from last index to first*****/

void printReverse(string s, int i)
{
  if(i < s.size()) 
    {  
      printReverse(s.substr(1), i);
      cout<<s[i];
    }    
    else
    {   
      return;  
    }
  }
 


   /****   Recursive power function, computes a^b, where b can be positive or negative*****/
int recPower(double a, int b)
  {   
    int i = b; //i = b, so int a can be multiplied int b times
     
    if (i == 0) //base 
      return 1;

    else //multiply A by B, B times
     {
       a *= b;
       return recPower(a, b); //recursive
       i--; //decrement i until it equals 0
     }
  }


   
   /****   Recursive string replace, replaces all instances of a character in a string with another character*****/
string recReplace(string s2, int i, char old, char neW)
   { 
       if(s2[i] == old) //search for old char
      {
        i = neW; //replace it
        i++; //iterate i
       }
    recReplace(s2, i, old, neW); //call function
    return s2;
   }



/****   Recursive list find   > Searches if x exists in list, returns true if found, false otherwise*****/
int recListFind(vector<int> v, int i, int x)
  {
    if(v[i] == x)    
      {       
        cout << x << " exists in the vector."<<endl;  
        i++;
        recListFind(v, i, x);
      }    
      return true;
    }



int main()
  {     
    cout << "PRINT REVERSE" << endl;
    cout << "----------" << endl;    
    string s1 = "hello world";    
    cout << "String: " << s1 << endl;    
    cout << "Reversed: ";    
    printReverse(s1, 0);       
    cout << endl;   
     
     
     /* Computes a^b (power function) */    
    cout << "POWER" << endl;    
    cout << "----------" << endl;    
    int a = 2, b = -3;    
    cout << a << "^" << b << " = ";    
    cout << recPower(a, b) << endl;    
    cout << endl;    
   
   
    /* Replaces a character in a string with a new one */    
    cout << "REPLACE" << endl;    
    cout << "----------" << endl;    
    string s2 = "-h-e-l-l-o-";    
    char oldChar = '-';    
    char newChar = ' ';   
    cout << "String: " << s2 << endl;    
    cout << "> Replace '" << oldChar << "' with '" << newChar << endl;    
    recReplace(s2, 0, oldChar, newChar);    
    cout << "String: " <<  s2 << endl;    
    cout << endl;    
   
   
    /* Searches for value in vector */    
    cout << "FIND" << endl;    
    cout << "----------" << endl;    
    int x = 7;    
    cout << "Does " << x << " exist in the vector? ";    vector<int> v = {5, 1, 6, 7, 9};    
    cout << recListFind(v, 0, 7) <<  endl;    
    cout << endl;    
  return 0;
}

【问题讨论】:

    标签: c++ recursion


    【解决方案1】:

    问题很简单,您正在使用b 执行recPower 函数。在函数中,如果 b 不为 0,则调用 recPower 时使用未修改的值 b(同时修改 a)。这总是会以无限递归结束,这会溢出你的堆栈。

    解决方案可能是:

    int recPower(double a, int b, int times) {        
      if (times == 0) 
        return a;
    
      else
        return b * recPower(a, b, --times); 
    }
    
    int recPower(double a, int b) {
        return recPower(a, b, b);
    }
    

    即使你解决了这个问题,你还有另一个问题。 b 可以为负数,根据您的逻辑,它将在递减的同时继续递归,直到溢出并返回 0。您将在第一个测试用例中导致这种情况。您应该考虑此函数中允许的类型,考虑将它们设为无符号,或明确处理否定的 b 情况。

    【讨论】:

    • 也适用于recReplace 函数。来自 clang-cl:警告:通过此函数的所有路径都将调用自身 [-Winfinite-recursion]
    • @AdrianMole 是的,但问题是(或至少应该是)仅关于第一个问题。我认为这些问题应该删除其余的。
    猜你喜欢
    • 2015-01-27
    • 1970-01-01
    • 2017-12-25
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多