c - 为什么我有时只会遇到段错误?

标签 c linked-list segmentation-fault gdb hashtable

我这里有一个哈希表,这个程序试图通过搜索单词的链接列表来查找变位词,这些单词的哈希值计算为其大写 ascii 值的总和。

我看不出这个段错误是如何发生的,更不用说理解 gdb 告诉我的内容了。很明显它发生在 anagramlookup() 中,但我看不出是如何发生的。

这是 gdb 的输出:

Program received signal SIGSEGV, Segmentation fault.
__strspn_sse2 ()
    at ../sysdeps/x86_64/multiarch/../strspn.S:53
53      ../sysdeps/x86_64/multiarch/../strspn.S: No such file or directory.
(gdb) backtrace
#0  __strspn_sse2 ()
    at ../sysdeps/x86_64/multiarch/../strspn.S:53
#1  0x0000000000400a0a in anagramlookup (
    word=0x7fffffffe7c0 "you") at thirdfailure.c:66
#2  0x0000000000400c17 in main () at thirdfailure.c:121
(gdb) frame 2
#2  0x0000000000400c17 in main () at thirdfailure.c:121
121             anagramlookup(search);
(gdb) print search
$1 = "you\000\000\177\000\000\221I\336\367\377\177\000\000\000\000\000\000\000\000\000\000\020\232\377\367\377\177\000\000\001", '\000' <repeats 15 times>, "\001\000\000\000\377\177\000\000\310\341\377\367\377\177", '\000' <repeats 37 times>

段错误不会一直发生。如果要查找的词是“post”,则一切正常。但是当它是例如“你”时,那就是我遇到段错误的时候。 “youu”不会给我段错误。我将如何解决问题?

这是我的代码:

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

struct Hash *hashTable = NULL;

struct Node{
    char val[100];
    struct Node *next;
};

struct Hash{
    int size;
    struct Node *head;
};

struct Node* newnode(char* word){

    struct Node *ptr = (struct Node*)malloc(sizeof(struct Node));
    strcpy(ptr->val, word);
    ptr->next = NULL;
    return ptr;

}

void insertHash(char* word){
    int index = hashmaker(word);

    struct Node *ptr = newnode(word);

    if(!hashTable[index].head){
        hashTable[index].head = ptr;
        hashTable[index].size = 1;
        return;
    }
    else{
        ptr->next = (hashTable[index].head);
        hashTable[index].head = ptr;
        hashTable[index].size++;
        return;
    }
}

void anagramlookup(char* word){
    int index = hashmaker(word);

    struct Node *ptr = hashTable[index].head;

    if(ptr == NULL){
        printf("we dont have any");
    }
    else{
        while((ptr!= NULL)){
            if(strlen(word)==strspn(word,ptr->val) &&
                strlen(word)==strspn(ptr->val,word) &&
                strlen(word)==strlen(ptr->val)){
                if(strcmp(word,ptr->val) != 0){
                    printf("\n%s", ptr->val );
                }
            }
        ptr = ptr->next;
        }
    }
}


int hashmaker(char* word){

    int toreturn = 0, i, len;
    len = strlen(word);

    for(i = 0; i < len; i++){
        toreturn += toupper(word[i]);
    }
    return toreturn;
}


int main(){

    char search[100];


    hashTable = (struct Hash *) malloc(sizeof(struct Hash));

    FILE* dict = fopen("words2", "r");
        if(dict == NULL) {
            printf("dict is null");
            exit(1);
        }
    // Read each line of the file, and print it to screen
    char wordo[128];
    while(fgets(wordo, sizeof(wordo), dict) != NULL) {
        printf("%s", wordo);
        wordo[strlen(wordo) - 1] = '\0';
        insertHash(wordo);
    }

    printf("now enter your search: ");
    fgets(search, sizeof(wordo), stdin);
    search[strlen(search) - 1] = '\0';
    anagramlookup(search);
    return 0;

}

最佳答案

hashTable 是一个struct Hash 的数组,但是它的内存还没有分配。您需要知道 hashTable 数组的大小并在通过索引访问它之前为其分配内存,例如。有表[索引]。如果hashTable的大小为n,按如下方式分配内存

hashTable = (struct Hash *) malloc(sizeof(struct Hash) * n);

如果您不知道尺码,请尝试使用安全号码。例如,下面的更改应该可以解决崩溃问题

hashTable = (struct Hash *) malloc(sizeof(struct Hash) * 3000);

关于c - 为什么我有时只会遇到段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35027944/

相关文章:

c - 程序无法正常运行

java - Entry语句的作用是什么?

c++ - Qt 在系统调用时崩溃?

c - 为什么下面会产生segmentation fault呢?

c - 复杂正则表达式的段错误 - Regex.h

c++ - 为什么编译不会失败?

c++ - C结构指针解引用速度

c - C 中的链表程序错误

c - 在 C 中添加到 FIFO 队列尾部时遇到问题,引用最后一个元素

c - 由于超时,Hackerrank 不接受我的代码,如何优化这一部分?