c++ - xstring第1331行的字符串下标超出范围

标签 c++ string linked-list mixing

第二个 while 循环在第二次迭代时在 xstring 行 1441 中抛出异常,这是它不应该做的。这是因为在第一次通过时:

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;
}

最佳答案

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;

关于c++ - xstring第1331行的字符串下标超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8717380/

相关文章:

c++ - tlb 文件是否具有关联架构?

java - 类的对象作为类内的实例变量

c - 将数组打印为字符串在末尾返回奇怪的字符

c# - 如何将 int 值转换为等效的泛型类型参数,例如 char?

c++ - 从原始图像中的 ROI 进行坐标变换

c++ - 处理多重继承时如何对齐指针?

c - 如何在C中的不同行中将字符串写入文件

java - 如何将字符串中每个单词的第一个字符大写

c - fgetc 无法加载文件的最后一个字符

java - 将元素添加到双链表