c - 对 `__ubsan_handle_nonnull_arg' 的 undefined reference

标签 c undefined-reference cs50 ubsan

<分区>

过去几天我一直在研究问题集拼写器,到目前为止,这就是我所拥有的。不幸的是,它没有编译,我有点迷路了。如果有人能帮助我并告诉我我做错了什么,我将不胜感激。

// Implements a dictionary's functionality    
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include "dictionary.h"
#define HASHTABLE_SIZE 65536
// A struct for a node
typedef struct node
{
    // Length is up to 45 + 1 for the Null
    char word[LENGTH + 1];
    // A pointer to the next node
    struct node *next;
}
node;
node *hashtable[HASHTABLE_SIZE];
node *head = NULL;

// Hashes words
// Thanks to the husband of reddit user delipity for providing the function
// https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/
int hash_it(char *word)
{
    unsigned int hash = 0;
    for (int i = 0, n = strlen(word); i < n; i++)
    {
        hash = (hash << 2) ^ word[i];
    }
    return hash % HASHTABLE_SIZE;
}

// A global var counter to keep track of all words in the dictionary
int counter = 0;
// A global var for the check function
int hashIndexForWord;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // Making a new variable with the capacity to store the word + 1 for the \0
    char copyWord[strlen(word) + 1];
    strcpy(copyWord, word);
    // A pointer to the beginning of a node
    node *cursor = hashtable[hash_it(copyWord)];
    while (cursor != NULL)
    {
        // Checks if the word is in the dictionary
        if (strcasecmp(cursor -> word, copyWord) == 0)
        {
            return true;
        }
        else
        {
            // Move the cursor to the next node
            cursor = cursor -> next;
        }
    }
    return false;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Load up a file and open it for reading
    FILE *file;
    file = fopen(dictionary, "r");
    char word[LENGTH + 1];
    // Making each of buckets of the table NULL
    for (int i = 0; i < HASHTABLE_SIZE; i++)
    {
        hashtable[i] = NULL;
    }
    int hashIndex;
    // Reading through the dictionary file, looking for a string
    // then putting it in a variable called word, until the EOF is reached
    while (fscanf(file, "%s", word) != EOF)
    {
        // Allocating a memory for a new node of size for a node
        node *new_node = malloc(sizeof(node));
        //node *head = malloc(sizeof(node));
        // Checking if malloc succeeded
        if (new_node == NULL)
        {
            // If it does return null, unload the dictionary and return false
            unload();
            return false;
        }
        // If it succeeds copy the word into the node
        strcpy(new_node->word, word);
        // Hashing the word and getting the index
        hashIndex = hash_it(word);
        // Pointing to the first element of the table
        new_node -> next = head;
        // Assigning the head to be the new element
        head = new_node;
        // Hashing the word and inserting a word to that place
        hashtable[hashIndex] = new_node;
        counter++;
    }
    // After the dictionary is loaded, close the file and return true
    fclose(file);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return counter;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // Make a cursor pointing to the head
    node *cursor = head;
    while (cursor != NULL)
    {
        // A temporary node pointing to cursor
        node *temp = cursor;
        // Move the cursor to next
        cursor = cursor -> next;
        // Free the temp
        free(temp);
    }
    return true;

    // When to return false? 
}

在所有的函数中,我得到了一些 undefined reference 和无效的符号索引,它们不允许代码编译:

undefined reference to `__ubsan_handle_nonnull_arg'

undefined reference to `__ubsan_handle_shift_out_of_bounds'

undefined reference to `__ubsan_handle_type_mismatch'

undefined reference to `__ubsan_handle_add_overflow'

undefined reference to `__ubsan_handle_divrem_overflow'

最佳答案

您没有提供有关如何编译代码的详细信息,但我怀疑您在 LDFLAGS 中缺少 -fsanitize=undefined

关于c - 对 `__ubsan_handle_nonnull_arg' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52333340/

相关文章:

c - 菜单程序的 Excel 数据文件

c - 如何在C文件中跳行?

C - 仅包含原型(prototype)的头文件

c - 在 C 中 - 循环内递增的变量不保留值 CS50

c - 使用 C 语言开源 RTSP/RTP 堆栈

c - 我怎样才能为 Windows 制作一个二进制文件,使非编码人员能够只获得一个可以提供给 gdb 的故障转储?

c++ - 使用 MinGW 和 Netbeans 的 ICU 项目 - 未定义引用

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c - 我如何处理 CS50 首字母中单词之前或之间的多个空格(更舒适)?

c - 基本 C 指针