简体   繁体   English

有没有办法解决分段错误? 我似乎无法弄清楚

[英]Is there a way to fix the segmentation fault ? I can't seem to figure it out

I'm not sure why I'm getting a segmentation fault in my code?我不确定为什么我的代码中出现分段错误?

 class Node {
    public:
        int data;
        Node *next;
};

void push_back(Node** head_ref, int new_data) {  
    Node* new_node = new Node(); 
  
    Node *last = *head_ref;
    new_node->data = new_data;  
    new_node->next = NULL;  

    if (*head_ref == NULL) {  
        *head_ref = new_node;  
        return;  
    }  
  
    while (last->next != NULL)  
        last = last->next;  
  
    last->next = new_node;
    
    return;  
}  

int getHash(string initials) {
    string digits;
    
    for (int i = 0; i < 3; i++) {
        switch(initials[i]) {
            case 'A':
            case 'B':
            case 'C':
                digits += "2";
                break;
            case 'D':
            case 'E':
            case 'F':
                digits += "3";
                break;
            case 'G':
            case 'H':
            case 'I':
                digits += "4";
                break;
            case 'J':
            case 'K':
            case 'L':
                digits += "5";
                break;
            case 'M':
            case 'N':
            case 'O':
                digits += "6";
                break;
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
                digits += "7";
                break;
            case 'T':
            case 'U':
            case 'V':
                digits += "8";
                break;
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
                digits += "9";
                break;
        }
    }
    
    return stoi(digits);
}

int getSize (Node* head) {
    int size = 0;
    bool not_null = true;
    Node *current = head;
    
    while (not_null) {
        if (current->next == NULL) {
            not_null = false;
            return size;
        } else {
            size++;
            current = current->next;
        }
    }
}

int main() {
    int count = 512;
    string initials;
    map<int, int> stats;
    Node* bitvec[count];
    for ( int i = 0; i < count; i++ ) {
        Node *data_node = new Node;
        data_node->data = 1;
        bitvec[i] = data_node;
    }

    for ( int i = 0; i < count; i++ ) {
        for (int j = 0; j < 3; j++) 
            initials.push_back((char)(rand() % 26 + 65));
        
        int hash = getHash(initials);
        
        cout << initials << " - " << hash << " - " << hash % count << endl;
        //cout <<"-"<< count<< endl;
        push_back(&(bitvec[hash % count]), 0);
        
        initials = "";
    }

    cout << "collision array" << endl;

    for ( int i = 0; i < count; i++ ) {
            if ( stats.count( getSize(bitvec[i]) ) <= 0 ) {
                    stats[ getSize(bitvec[i]) ] = 1;
            } else {
                    stats[ getSize(bitvec[i]) ]++;
            }
            cout << setw( 2 ) << getSize(bitvec[i]) << " ";
            if ( ( i + 1 ) % 25 == 0 ) {
                    cout << endl;
            }
    }

    int total = 0;
    cout << endl;
    for ( map<int, int>::iterator it = stats.begin( ); it != stats.end( ); it++ ) {
            if ( it->first != 0 ) {
                    total += it->second;
            }
            cout << it->first << " collisions occurred #" << it->second << endl;
    }

    cout << "Max number of collisions is: " << ( --stats.end( ) )->first << endl;
    cout << "Total collisions: " << total << endl;
    float avg = (float) count / total;
    cout << "average search: " << avg << endl;
  
    return 0;
}

In mainmain

for ( int i = 0; i < count; i++ ) {
    Node *data_node = new Node;
    data_node->data = 1;
    // whoops! Didn't set data_node->next = NULL.
    bitvec[i] = data_node;
}

This mistake means the while (last->next != NULL) and similar tests later in the code fail.这个错误意味着代码中的while (last->next != NULL)和类似的测试失败。 This means my comment under the question nailed the location of the bug, but got the cause wrong.这意味着我在问题下的评论确定了错误的位置,但原因是错误的。 Such is life.这就是生活。 Should have taken me up on the bet.应该带我去打赌。

This can be simplified to这可以简化为

for ( int i = 0; i < count; i++ ) {
    bitvec[i] = new Node{1, NULL};
}

Which uses Aggregate Initialization to make sure everything is initialized.它使用聚合初始化来确保一切都已初始化。

This prevents the program from crashing.这可以防止程序崩溃。 I have not checked to see if the output is correct.我还没有检查 output 是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM