2012-06-05 2 views
0

프로젝트를 통해 괄호로 둘러싸인 단어 표현을 사용하고 입력 파일에서 발생 위치를 확인할 수 있습니다. 같은 (블루와 블랙) 파일 1 (다크 AND (하늘과 (꽃이나 장미))) 파일 2세그멘테이션 결함을 일으키는 C++ 재귀 (AND 및 OR 검색어)

는 (블루 OR 같은 더 간단한 표현을 때 내 프로그램이 작동

블랙) 하지만 더 복잡한 작업을 수행 할 때 세그먼트 화 오류가 발생합니다.

이 임 오류가 doQuery 또는 doMultipleQuery 중 하나에서 발생해야해야합니다 (I은 포함하지 않았다 포함하고 중요하지 않은 기능) 오류를 포함하는 .cpp 파일입니다 같은

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); 
} 
+0

여기에 게시하기 전에 코드를 들여 쓰도록하십시오. 그렇지 않은 코드는 읽기가 어렵습니다. – Dave

+0

[Shunting-yard algorithm] (http://en.wikipedia.org/wiki/Shunting-yard_algorithm)을 살펴볼 수 있습니다. 또한 세분화 오류는 위의 코드에없는 것 같은 잘못된 메모리 관리를하고 있음을 의미합니다. 예를 들어'doQuery '를 어떻게 호출할까요? (결과로 전달되는 것) – Cameron

+2

http://sscce.org/를 방문하십시오. 빈 텍스트 파일에 코드를 복사하고 컴파일하면'#include'와 클래스 정의가 누락되어 실패합니다 (즉,'MultiQuery'와'WordSearch'). – moshbear

답변

0

이있을 수있는 다른 오류를 코드가 매우 복잡하고 복잡하지만, 결과 배열에 메모리를 할당하지 않는다는 것이 중요합니다. 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 

std::vectorpush_back 결과를 사용하는 것이 좋을 것이다.

관련 문제