C++ 为什么这个程序在调用函数时会出现分段错误?
这是我程序的一部分,当我运行这个程序时,我得到一个分段错误。我已经把范围缩小到: checkBase(ptr1、ptr2) 我把这两个都作为指针传递。它们被声明为char*,这是一个运行时错误,而不是编译时错误。 文件包含 www.spam.google.com 在本例中,ptr1=www.google.com和ptr2=spam.google.comC++ 为什么这个程序在调用函数时会出现分段错误?,c++,segmentation-fault,C++,Segmentation Fault,这是我程序的一部分,当我运行这个程序时,我得到一个分段错误。我已经把范围缩小到: checkBase(ptr1、ptr2) 我把这两个都作为指针传递。它们被声明为char*,这是一个运行时错误,而不是编译时错误。 文件包含 www.spam.google.com 在本例中,ptr1=www.google.com和ptr2=spam.google.com while(inf){ count++; getline(inf, line); //cout << "*
while(inf){
count++;
getline(inf, line);
//cout << "*******" << count << "*******" << endl <<line << endl;
p = new char[line.length()+1];
strcpy(p, line.c_str());
if(strstr(p, "href")){
ptr = strstr(p, "href");
while(ptr[0]!='\0'){
ptr += 1;
if(ptr[0] == 'w' && ptr[1] == 'w' && ptr[2] == 'w'){
cout << ptr << endl;
ptr = strtok(ptr, "\"");
cout << "add1 " << ptr << endl;
add1 = ptr;
ptr1 = ptr;
ptr = strtok(NULL, "> ");
add2 = ptr;
ptr2 = ptr;
cout << "ptr1: " << ptr1 << endl << "ptr2: " <<ptr2 << endl;
if(add1 == add2)
cout << "There is an exact match at line: " << count << endl << line << endl;
else{
cout << "in else" << endl;
checkBase(ptr1, ptr2); //THIS GIVES A SEGMENTATION FAULT
}
}
}
}
}
void checkBase(char *add1, char *add2){
cout << "here" << endl;
char *base1[1000000], *base2[1000000];
int count1 = 0, count2 = 0;
base1[count1] = strtok(add1, ".");
while(base1[count1] != NULL){
count1++;
base1[count1] = strtok(NULL, ".");
cout << base1[count1] << endl;
}
base2[count2] = strtok(add2, ".");
while(base2[count2] != NULL){
count2++;
base2[count2] = strtok(NULL, ".");
}
cout << base2[count2-1] << endl;
if(((strcmp(base1[count1-1],base2[count2-1])) != 0) && (strcmp(base1[count1-2], base2[count2-2]) != 0)){
//if((strcmp(base1[count1-1], base2[count2-1]) != 0)){
cout << "Bases do not match: " << endl
<< base1[count1-2] << "." << base1[count1-1] << " and "
<< base2[count2-2] << "." << base2[count2-1] << endl;
//}
}
else{
cout << "Bases match: " << endl
<< base1[count1-2] << "." << base1[count1-1] << " and "
<< base2[count2-2] << "." << base2[count2-1] << endl;
}
}
while(inf){
计数++;
getline(inf,line);
//库特
毫无疑问,这会导致堆栈溢出。堆栈大小有限,创建大于几kb的数组是个坏主意。请尝试在堆上分配它们,例如vector base1(1000000)
您还应该计算所需的确切大小并分配那么多,或者将_推回向量
毫无疑问,这会导致堆栈溢出。堆栈大小有限,创建大于几kb的数组是个坏主意。请尝试在堆上分配它们,例如vector base1(1000000)
您还应该计算所需的确切大小并分配那么多,或者将u推回到向量上。除了@Neil Kirkwell已经提到的堆栈溢出之外,还有几个问题
当循环仅以base1[count1]!=NULL为条件时,这些值不应为空;还应确保count1小于数组中的元素数
如果count2或count1是0或1,您将尝试引用索引-1和-2…不太好
使用strrchr向后搜索,使您的生活更轻松
完全构建这些数组是浪费的,因为您似乎只关心最后两个标记,每个标记中只需要两个指针
i、 e
char *one_a = NULL, *one_b = NULL, *two_a=NULL, *two_b = NULL;
char *temp = strtok(add1, ".");
while (temp) {
one_b = one_a;
one_a = temp
temp = strtok(NULL, ".");
}
char *temp = strtok(add2, ".");
while (temp) {
two_b = two_a;
two_a = temp
temp = strtok(NULL, ".");
}
//now just compare one_a with two_a and one_b with two_b and you're done.
除了@Neil Kirkwell已经提到的堆栈溢出之外,还有几个问题
当循环仅以base1[count1]!=NULL为条件时,这些值不应为空;还应确保count1小于数组中的元素数
如果count2或count1是0或1,您将尝试引用索引-1和-2…不太好
使用strrchr向后搜索,使您的生活更轻松
完全构建这些数组是浪费的,因为您似乎只关心最后两个标记,每个标记中只需要两个指针
i、 e
char *one_a = NULL, *one_b = NULL, *two_a=NULL, *two_b = NULL;
char *temp = strtok(add1, ".");
while (temp) {
one_b = one_a;
one_a = temp
temp = strtok(NULL, ".");
}
char *temp = strtok(add2, ".");
while (temp) {
two_b = two_a;
two_a = temp
temp = strtok(NULL, ".");
}
//now just compare one_a with two_a and one_b with two_b and you're done.
在调试器的监视下运行它,我打赌你会很快发现。你识别的一行代码会让你出错?是的,这就是不在这里的代码。请为你的checkBase
函数添加代码。许多segfaults是由于使用的指针没有正确初始化为有效的饼图而导致的内存不足。在我看到你的checkBase
函数的代码之前,我只能推测这是个问题。添加你实际声明变量的代码部分,并检查ptr1和ptr2是否为null…在调试器的监视下运行它,我敢打赌你会很快发现。还有你编写的一行代码确定这是您的错误吗?是的,这是不在这里的代码。请为您的checkBase
函数添加代码。许多SEGFULT是由于使用未正确初始化到有效内存段的指针造成的。在看到您的checkBase
函数的代码之前,我只能推测这是问题所在。A添加代码中实际声明变量的部分,并检查ptr1和ptr2是否为null。。。