c++ - 为什么这个程序在调用函数时会出现段错误?

标签 c++ segmentation-fault

这是我程序的一部分,当我运行这个程序时我遇到了段错误。我已将范围缩小到以下行:

checkBase(ptr1, ptr2)

我将这两个作为指针传递。它们被声明为 char* 并且它是一个运行时错误而不是编译时。 文件包含

< a href = "http://www.google.com"> www.spam.google.com < /a >

在这种情况下 ptr1 = www.google.com 和 ptr2 = spam.google.com

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

我不知道为什么会出现段错误。

最佳答案

char *base1[1000000], *base2[1000000];

毫无疑问,这是导致堆栈溢出的原因。堆栈的大小有限,创建大小超过几 kb 的数组不是一个好主意。尝试在堆上分配它们,例如 vector<char *> base1(1000000)

您还应该计算所需的确切大小并分配那么多,或者在 vector 上 push_back。

关于c++ - 为什么这个程序在调用函数时会出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18345069/

相关文章:

c++ - 在 Ubuntu 终端中编译时 OpenGL 引用的问题

c++ - 在减少模板使用的同时优化性能

python manage.py runserver 段错误

c++ - 传递右值加注不能绑定(bind)到左值

c++ - 从 dllexported 函数返回 char*

c++ - C++ 中 pop_front() 的段错误

c - 查找 C 中的段错误

c - 从原始文件读取时出现段错误

c++ - 将可变数量的参数传递给别名函数

c++ - Boost::filesystem、std::sort:在排序过程中保留信息时遇到问题