c++ recursion causing segmentation faults (AND and OR queries with words) -


my project makes me take parenthesized expressions of words , check input files occur. such ( blue , black ) file1 ( dark , ( sky , ( flower or roses ) ) ) file2

etc

my program works when comes more simple expressions, ( blue or black ) run in segmentation faults when perform more complex operations.

this .cpp file contains errors im sure error must occur in either doquery or domultiplequery (i didn't include includes , unimportant functions)

void wordsearch::doquery(string query,string *result,int &size){   int temp = 0, = 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 , blue )     doquery(query, result, size);    if (oper == "and"){ // files need include both     if (op1_size < 5 && op2_size >= 5){ // ( pink or ( blue , 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 , 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 , red ) or ( pink , 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 need include 1   if (op1_size < 5 && op2_size >= 5){ // ( pink or ( blue , 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 , 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 , red ) or ( pink , 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 other errors code complex , convoluted, 1 sticks out never allocate memory result arrays.
@ domultiplequery:

string *temp1, *temp2; int index1 = 0, index2 = 0, size1 = 0, size2 = 0;  if (thisquery.getsize(query) <= 5) // ( red , blue )     doquery(query, result, size);  if (oper == "and"){ // files need include both     if (op1_size < 5 && op2_size >= 5){ // ( pink or ( blue , black ) )         string op1temp = "( " + operand1 + " )";     doquery(op1temp, temp1, size1);   // <-- temp1 uninitialized here        domultiplequery(operand2, temp2, size2);  // <-- temp2 uninitialized here 

it better use std::vector , push_back results onto it.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -