简体   繁体   English

C ++递归导致分段错误(AND和OR查询单词)

[英]c++ recursion causing segmentation faults (AND and OR queries with words)

My project makes me take parenthesized expressions of words and check the input files for where they occur. 我的项目使我采用带括号的单词表示形式,并检查输入文件中出现的位置。 Such as ( Blue AND Black ) file1 ( Dark AND ( Sky AND ( Flower OR Roses ) ) ) file2 例如(蓝色AND黑色)file1(黑暗AND(天空AND(花或玫瑰)))file2

etc 等等

my program works when it comes to more simple expressions, like ( Blue OR Black ) but i run in segmentation faults when I perform more complex operations. 当涉及到更简单的表达式(例如(Blue或Black))时,我的程序可以工作,但是当我执行更复杂的操作时,我会遇到分段错误。

this is .cpp file that contains the errors Im sure the error must occur in either doQuery or doMultipleQuery (I didn't include includes and unimportant functions) 这是包含错误的.c​​pp文件。确保该错误一定发生在doQuery或doMultipleQuery中(我没有包括include和不重要的函数)

void WordSearch::doQuery(string query,string *result,int &size){
  int temp = 0, i = 0;

  MultiQuery thisQuery;
  thisQuery.parse_string(query);
  string word1 = thisQuery.getOperand1();
  string word2 = thisQuery.getOperand2();
  string op = thisQuery.getOperator();

  if (thisQuery.getSize(query) < 5) {
    List *list1;
    int index = 0, list1_size = 0;
    string temp[MAX_SIZE];  
    list1 = wordlist->search(word1);
    if (list1 != NULL){
      list1->all(temp, list1_size);
      while(index < list1_size){
        result[size] = temp[index];
        size++;
    index++;
      }

if(size == 0){
      result[size]="No Such File";
      size++;
    }

sort(result,result+size);
  }
  else
  cout<<"Wrong query";
return;
}

if (op=="AND") {
  size=0;
  and_operation(wordlist,word1,word2,size,result);
  sort(result,result+size);
} 
else if(op=="OR") {
  size=0;
  or_operation(wordlist,word1,word2,size,result);
  sort(result,result+size);
}
return;
} 


void WordSearch::doMultipleQuery(string query,string *result,int &size){
  MultiQuery thisQuery;
  thisQuery.parse_string(query);
  string operand1 = thisQuery.getOperand1();
  string operand2 = thisQuery.getOperand2();
  string oper = thisQuery.getOperator();
  int op1_size = thisQuery.getSize(operand1);
  int op2_size = thisQuery.getSize(operand2);
  string *temp1, *temp2;
  int index1 = 0, index2 = 0, size1 = 0, size2 = 0;

  if (thisQuery.getSize(query) <= 5) // ( Red AND Blue )
    doQuery(query, result, size);

  if (oper == "AND"){ // Files need to include both
    if (op1_size < 5 && op2_size >= 5){ // ( Pink OR ( Blue AND Black ) )
  string op1Temp = "( " + operand1 + " )";
      doQuery(op1Temp, temp1, size1);       
  doMultipleQuery(operand2, temp2, size2);
  while (index2 < size2) {
    while (index1 < size1){
      if (temp2[index2] == temp1[index1]){
    string tempString = temp2[index2];
    result[size] = tempString;
        size++;
      }
      index1++;
    }
      index2++;
      }
    }     
    if (op1_size >= 5 && op2_size < 5){ // ( ( Blue AND Black ) OR Pink )
  string op2Temp = "( " + operand2 + " )";
      doQuery(op2Temp, temp2, size2);   
  doMultipleQuery(operand1, temp1, size1);
  while (index1 < size1) {
    while (index2 < size2){
      if (temp1[index1] == temp2[index2]){
    string tempString = temp1[index1];
    result[size] = tempString;
    size++;
      }
      index2++;
    }
      index1++;
    }
  }   
  if (op1_size >= 5 && op2_size >= 5){ // ( ( Flower AND Red ) OR ( Pink AND Blue ) )
    doMultipleQuery(operand1, temp1, size1);
    doMultipleQuery(operand2, temp2, size2);
  while (index1 < size1) {
    while (index2 < size2){
      if (temp1[index1] == temp2[index2]){
    string tempString = temp1[index1];
    result[size] = tempString;
    size++;
      }
      index2++;
        }
        index1++;
  }
}  
}   

if (oper == "OR")   { // Files only need to include one
  if (op1_size < 5 && op2_size >= 5){ // ( Pink OR ( Blue AND Black ) )
  string op1Temp = "( " + operand1 + " )";
  doQuery(op1Temp, temp1, size1);
  doMultipleQuery(operand2, temp2, size1);
  while (index2 < size2){
result[size] = temp2[index2];
index2++;
size++;
  }
  while (index1 < size1){
result[size] = temp1[index1];
index1++;
size++;
  }
}   
if (op1_size >= 5 && op2_size < 5){ // ( ( Blue AND Black ) OR Pink )
  string op2Temp = "( " + operand2 + " )";
  doQuery(op2Temp, temp2, size2);   
  doMultipleQuery(operand1, temp1, size1);
  while (index2 < size2){
result[size] = temp2[index2];
index2++;
size++;
  }
  while (index1 < size1){
result[size] = temp1[index1];
index1++;
size++;
  }
}   
if (op1_size >= 5 && op2_size >= 5){ // ( ( Flower AND Red ) OR ( Pink AND Blue ) )
  doMultipleQuery(operand1, temp1, size1);
  doMultipleQuery(operand2, temp2, size2);
  while (index2 < size2){
result[size] = temp2[index2];
index2++;
size++;
  }
  while (index1 < size1){
result[size] = temp1[index1];
index1++;
size++;
  }
}
}   
sort(result,result+size);
}

There may be other errors as your code is very complex and convoluted, but one that sticks out is that you never allocate memory for the result arrays. 由于您的代码非常复杂且令人费解,因此可能还会出现其他错误,但最引人注意的是您永远不会为结果数组分配内存。
Look at doMultipleQuery : 看一下doMultipleQuery

string *temp1, *temp2;
int index1 = 0, index2 = 0, size1 = 0, size2 = 0;

if (thisQuery.getSize(query) <= 5) // ( Red AND Blue )
    doQuery(query, result, size);

if (oper == "AND"){ // Files need to include both
    if (op1_size < 5 && op2_size >= 5){ // ( Pink OR ( Blue AND Black ) )
        string op1Temp = "( " + operand1 + " )";
    doQuery(op1Temp, temp1, size1);   // <-- temp1 is uninitialized here       
doMultipleQuery(operand2, temp2, size2);  // <-- temp2 is uninitialized here

It would be better to use std::vector and push_back the results onto it. 这将是更好地使用std::vectorpush_back结果到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM