微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 int main 之外放置函数时的非法指令 4

如何解决在 int main 之外放置函数时的非法指令 4

我刚刚开始学习 C 语言,我的一个程序遇到了问题。

执行时出现错误:“非法指令 4”:./dictionary large.txt

Large.txt 是一个包含 143091 个按字母顺序排序的单词的文件,每个单词从一个新行开始。我正在尝试将它们全部加载到哈希表中,如果所有单词都加载成功,则返回 true。

如果 bool load() 中的代码在 int main 内并且 load() 不存在,则此代码我有用。但是,一旦我将它放在 load() 函数中并从 main 中调用它,我就会收到一个错误

我很感激这方面的帮助,因为关于非法指令的线程并不多。

这是我的代码

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

// Maximum length for a word
// (e.g.,pneumonoultramicroscopicsilicovolcanoconiosis)
#define LENGTH 45

// Number of letters in the english alphabet
#define ALPHABET_LENGTH 26

// Default dictionary
#define DICTIONARY "large.txt"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
} node;

// Number of buckets in hash table
const unsigned int N = ALPHABET_LENGTH;

// Hash table
node *table[N];

// Load function
bool load(char *dictionary);

// Hash function
int hash(char *word);

int main(int argc,char *argv[])
{
    // Check for correct number of args
    if (argc != 2 && argc != 3)
    {
        printf("Usage: ./speller [DICTIONARY] text\n");
        exit(1);
    }

    // Determine which dictionary to use
    char *dictionary = (argc == 3) ? argv[1] : DICTIONARY;

    bool loaded = load(dictionary);

    // Todo: free hashtable from memory

    return 0;
}

bool load(char *dictionary)
{
    // Open dictionary for reading
    FILE *file = fopen(dictionary,"r");
    if (file == NULL)
    {
        printf("Error 2: Could not open %s. Please call customer service.\n",dictionary);
        exit(2);
    }

    // Initialize array to NULL
    for (int i = 0; i < N; i++)
        table[i] = NULL;

    // Declare and initialize variables
    unsigned int char_count = 0;
    unsigned int word_count = 0;
    char char_buffer;
    char word_buffer[LENGTH + 1];
    int hash_code = 0;
    int prevIoUs_hash_code = 0;

    // Declare pointers
    struct node *first_item;
    struct node *current_item;
    struct node *new_item;

    // Is true the first time the while loop is ran to be able to distinguish between hash_code and prevIoUs_hash_code after one loop
    bool first_loop = true;

    // Count the number of words in dictionary
    while (fread(&char_buffer,sizeof(char),1,file))
    {

        // Builds the word_buffer by scanning characters
        if (char_buffer != '\n')
        {
            word_buffer[char_count] = char_buffer;

            char_count++;
        }
        else
        {
            // Increases word count each time char_buffer == '\n'
            word_count += 1;

            // Calls the hash function and stores its value in hash_code
            hash_code = hash(&word_buffer[0]);

            // Creates and initializes first node in a given table index
            if (hash_code != prevIoUs_hash_code || first_loop == true)
            {
                first_item = table[hash_code] = (struct node *)malloc(sizeof(node));
                if (first_item == NULL)
                {
                    printf("Error 3: memory not allocated. Please call customer service.\n");
                    return false;
                }

                current_item = first_item;
                strcpy(current_item->word,word_buffer);
                current_item->next = NULL;
            }
            else
            {
                new_item = current_item->next = (struct node *)malloc(sizeof(node));
                if (new_item == NULL)
                {
                    printf("Error 4: memory not allocated. Please call customer service.\n");
                    return false;
                }

                current_item = new_item;
                strcpy(current_item->word,word_buffer);
                current_item->next = NULL;
            }

            // Fills word buffer elements with '\0'
            for (int i = 0; i < char_count; i++)
            {
                word_buffer[i] = '\0';
            }

            // Signals the first loop has finished.
            first_loop = false;

            // Clears character buffer to keep track of next word
            char_count = 0;

            // Keeps track if a new table index should be initialized
            prevIoUs_hash_code = hash_code;
        }
    }

    return true;
}

// Hash in order of: 'a' is 0 and 'z' is 25
int hash(char *word_buffer)
{
    int hash = word_buffer[0] - 97;

    return hash;
}

先谢谢你!

克里斯

解决方法

您应该对 node *table[ALPHABET_LENGTH]; 声明使用 table 而不是 node *table[N];

常量宏和 const 变量之间存在差异,宏可以在常量表达式中使用,例如根据您的用例绑定的全局数组,而 const 变量不能。

当您 can see here 时,您说您正在使用的编译器 gcc,没有编译器标志,会发出一条错误消息:

error: variably modified 'table' at file scope

您可以在 "static const" vs "#define" vs "enum" 中阅读有关这些差异和用例的更多信息,它有更多的主题,例如 staticenum,但是很好地理解这些概念之间的差异。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。