2012-01-03 72 views
0

第二个while循环在第二次迭代中在xstring行1441中抛出异常,它不应该这样做。这是因为第一遍:xstring的第1331行的字符串下标超出范围

Testthis->NAME[n] = 'B' 
Testthis->NAME[n+1] = 'O' 
Testthis->NAME = "BOD MQL" 

都从调试cout语句验证。我沉迷于这一个。

struct Node * ProbableMatch(struct Node * Look_in, int MaxNodes, char Findthis[64]){ 
system("cls"); 
int i=0,proba=100,CurrentHigh=100; 
size_t pos1=1,n1=0,n2=0,pos2=1; 
//char s[64]; 
struct Node * CurrentHighProb; 
struct Node * Testthis; 
CurrentHighProb=new(Node); 
Testthis=new(Node); 
Testthis=Look_in; 
//for(i=0;i==MaxNodes;i++) 
while(!Testthis || i!=ccounter) 
{ 
    gotoxy(0,0); 
    int n=0; 
    n1=sizeof(Look_in->NAME); 
    n2=sizeof(Findthis); 
    string str1; 
    char str2[64]; 
    cout<<Testthis->NAME[n]<<endl<<Testthis->NAME<<endl; 
    cout<<Testthis->NAME[n+1]<<endl; 

    while(Testthis->NAME[n]!='\0'){ //mannually coverts the strings since I am having hell with the inbuilt functions 
     cout<<Testthis->NAME[n]; 
     str1[n]=Testthis->NAME[n]; 
     if(Testthis->NAME[n+1]=='\0'){str1[n+1]='\0';}//makes NULL terminated strings 
     n++; 
    }//end of while 

    //strcpy_s(cstr, str1.length()); 
    //strcpy_s(str1,sizeof(Look_in->NAME),Look_in->NAME); 
    //strcpy_s(str2,sizeof(Findthis),Findthis); 
    //char * cstr1 = new char[str1.length()+1]; 
    cout<<str1.compare(pos1,n1,Findthis,0,n2)<<" is the value of the string compare "<<endl; 
    proba=str1.compare(pos1,n1,Findthis,0,n2);//compares Findthis to the varibles read from the database from string matches 

    //DEBUG STUFF 
    cout<<endl<<sizeof(Look_in->NAME)<<" sizeof(Look_in->NAME)"<<Look_in->NAME<<endl; 
    cout<<sizeof(Findthis)<<" sizeof(Findthis)"<<Findthis<<endl; 
    cout<<sizeof(str1)<<" "<<str1<<" string1 string2 "<<str2<<" "<<sizeof(str2)<<endl; 
    //cout<<sizeof(str1)<<" reinterpret_cast< char *>(str1)"<<endl; 
    system("PAUSE"); 
    cout<<"\n Probability of "<<Look_in->NAME<<" matching "<<Findthis<<" is "<<proba<<" ."<<endl; 
    //ENDOFDEBUG FOR FUNCTION 
    Testthis->prob=proba; 
    if(proba==0) 
    { 
     cout<<"proba equals zero, match found returning "<<Look_in<<endl; 
     //delete[] cstr1; 
     return Testthis; // returns the pointer of the varible that best matched the search 
    } 
    if(proba<CurrentHigh){ 
     CurrentHigh=proba; 
     CurrentHighProb=Testthis; 
    } 
    i++; 
    Testthis=Testthis->next; 
} 
cout<<"Perfect match not found after "<<i<<" number of searches, returning this highest probable match "<<CurrentHighProb<<" "<<proba<<endl; 

system("PAUSE"); 
//delete[] cstr1; 
return CurrentHighProb; 
} 
+0

'Testthis = new(Node); Testthis = Look_in;'你好,内存泄漏。如果您有兴趣学习如何编写正确的C++代码,请考虑获得[良好的入门C++书](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 – 2012-01-03 19:18:49

+0

谢谢,但在这一点上,我只是试图让函数找到正确的条目,之后完成我将切换到只传递程序终止时删除的头指针。 – John 2012-01-03 21:14:55

回答

1
string str1; 

声明了一个零长度std::string

str1[n]=Testthis->NAME[n]; 

引用str1的(不存在的)第n个元素。

也许你的意思是创建一个string具有一定的长度:

string str1(n1, ' '); 

或者,也许你的意思是在复制过程中分配的字符串数据:

str1.push_back(Testthis->NAME[n]); 

有其他错误的代码,但这个可能会产生你描述的异常。

您可以简单地避免所有这些错误。用这个替换内部while循环:

str1 = Testthis->NAME; 
+0

谢谢。上一次我做了任何编程在98' – John 2012-01-03 19:25:24

+0

这个'str1 = Testthis-> NAME;'给了我一个转换错误,不能将char [64]转换为std :: string [64],这些数据类型是否可以兼容? – John 2012-01-03 19:34:29

+0

嗯。在你发布的代码中,你将'str1'声明为'std :: string str1;'。你把它改成'std :: string str1 [64]'?在第一种情况下,这些类型是兼容的。第二,他们不是。 – 2012-01-03 19:41:11