【问题标题】:Segmentation fault while inserting into a hash table插入哈希表时出现分段错误
【发布时间】:2014-03-28 11:34:44
【问题描述】:

我希望能够使用我的 getNextWord 函数返回指向文件中下一个单词的指针。我想我在插入时遇到了段错误,但我就是想不通。对此的任何帮助都会非常好。此外,我可能应该找到一种更好的方法来获取我的 hash_table_size,而不是增加文件中单词总数然后倒带的计数。如何使大小自动增长?

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int hash_table_size;

char* getNextWord(FILE* fd) {
    char c;
    char buffer[256];
    int putChar = 0;
    while((c = fgetc(fd)) != EOF) {
        if(isalnum(c)) break;
    }
    if(c == EOF) return NULL;

    buffer[putChar++] = c;

    while((c = fgetc(fd)) != EOF) {
        if(isspace(c) || putChar >= 256 -1) break;

        if(isalnum(c))
            buffer[putChar++] = c;
    }

    buffer[putChar] = '\0';
    return strdup(buffer);
}

struct node {
    struct node *next;
    int count;
    char* key;
};

struct list {
    struct node *head;
    int count;
};

struct list *hashTable = NULL;

/*
 * djb2 hash function
*/
unsigned int hash(unsigned char *str) {
    unsigned int hash = 5381;
    int c;

    while(c == *str++)
        hash = ((hash << 5) + hash) + c;

    return (hash % hash_table_size);
}

struct node* createNode(char *key) {

    struct node *new_node;
    new_node = (struct node *)malloc(sizeof(struct node));
    strcpy(new_node->key, key);
    new_node->next = NULL;
    return new_node;
}

void hashInsert(char *str) {
    int hash_dex = hash(str);
    struct node *new_node = createNode(str);

    if(!hashTable[hash_dex].head) {
        hashTable[hash_dex].head = new_node;
        hashTable[hash_dex].count = 1;
        return;
    }

    new_node->next = (hashTable[hash_dex].head);

    hashTable[hash_dex].head = new_node;
    hashTable[hash_dex].count++;
    return;
}

void display() {
    struct node *current;
    int i;
    while(i < hash_table_size) {
        if(hashTable[i].count == 0)
            continue;
        current = hashTable[i].head;
        if(!current)
            continue;
        while(current != NULL) {
            char tmp[256];
            strcpy(tmp, current->key);
            printf("%s", tmp);
            current = current->next;
        }
    }
    return;
}

int main(int argc, char *argv[]) {

    if (argc != 2) {
        printf("Usage: ./hashFile textfile\n");
    }
    else {
        FILE *file = fopen(argv[1], "r");

        if(file == 0) {
            printf("Could not open file\n");
        }
        else {

            char *new_word;
            while((new_word = getNextWord(file)) != NULL) {
                hash_table_size++;
            }
            rewind(file);
            hashTable = (struct list *)calloc(hash_table_size, sizeof(struct list));
            while((new_word = getNextWord(file)) != NULL) {
                hashInsert(new_word);
            }
            display();
            fclose(file);
            }
        }
        return 0;
}

【问题讨论】:

    标签: c file pointers hash segmentation-fault


    【解决方案1】:
        int c;
    
        while(c == *str++)
            hash = ((hash << 5) + hash) + c;
    

    c 此处未初始化。正如display 函数中的i 一样。请启用所有编译器警告并修复它们。

    还有:

    char c;
    
    char buffer[256];
    int putChar = 0;
    while((c = fgetc(fd)) != EOF) {
        if(isalnum(c)) break;
    }
    

    c 的类型必须是 int 而不是 char

    【讨论】:

      【解决方案2】:

      改变

      while(c == *str++)
      

      while(c = *str++)
      

      【讨论】:

        猜你喜欢
        • 2018-05-09
        • 2021-01-19
        • 2019-12-10
        • 2016-03-03
        • 2012-04-23
        • 2013-12-25
        • 1970-01-01
        • 2018-06-03
        相关资源
        最近更新 更多