недопустимая запись / чтение размером 1 и XX байтов в 1 блоках однозначно теряются в записи потери - PullRequest
0 голосов
/ 06 мая 2020

У меня возникают эти проблемы в моем коде, и я ищу решения здесь, в StackOverflow и на другом сайте, но не могу понять, почему это происходит.

Мне нужно реализовать ha sh таблицу, и я опубликую здесь функции моего кода, которые у меня возникают, и сигнализирую, где они возникают.

** OBS: Когда я пытаюсь закрыть файл, открытый в функции ЗАГРУЗИТЬ, я получаю сообщение об ошибке:

 Munmap_chunk: invalid pointer

это происходит, когда слово * пневмоноультрамикроскопический силиковулканокониоз ***

// Hashes word to a number
unsigned int hash(const char *word)
{
    int sum = 0;

    for (int i = 1; i <= strlen(word); i++) ****Invalid read of size 1**
    {
        sum = (sum + (i * tolower(word[i]))); ****Invalid read of size 1**
    }

    if (sum > N)
    {
        sum = sum % strlen(word);
    }

    return sum;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // TODO

    FILE *file = fopen(dictionary, "r");****552 bytes in 1 blocks are still reachable in loss record 5 of 5**
    if (file == NULL)
    {
        printf("Could not open this file.\n");
        return 1;
    }
    else
    {
        for (int i = 0; i < N; i++)
        {
            table[i] = NULL;
        }

        char *word1 = malloc(sizeof(LENGTH + 1));

        while (fscanf(file, "%s", word1) != EOF)****Invalid write of size 1**
        {

            node *n = malloc(sizeof(node));
            if (n == NULL)
            {
                free(n);
                return 1;
            }

            strcpy(n->word, word1);****Invalid read of size 1**
            n->next = NULL;

            unsigned int index = hash(word1);

            if (table[index] == NULL)
            {
                table[index] = n;
            }
            else
            {
                n->next = table[index];
                table[index] = n;
            }
        }

        free(word1);
    }

    return true;
}

unsigned int size(void)
{
    // TODO

    int count = 0;
    node *cursor = malloc(sizeof(node));
    cursor->next = NULL;
                                 ****56 bytes in 1 blocks are definitely lost in loss record 1 of 5**
    for (int i = 0; i < N; i++)
    {

        if (table[i] != NULL)
        {
            cursor = table[i];

            while (cursor != NULL)
            {
                count++;
                cursor = cursor->next;
            }
        }
    }

    free(cursor);
    return count;
}
bool unload(void)
{
    // TODO

    int count = size();
    int count1 = 0;
    node *cursor = malloc(sizeof(node));
    node *tmp = malloc(sizeof(node));
    cursor->next = NULL;****56 bytes in 1 blocks are definitely lost in loss record 3 of 5**
    tmp->next = NULL;****56 bytes in 1 blocks are definitely lost in loss record 4 of 5**

    for (int i = 0; i < N; i++)
    {
        if (table[i] != NULL)
        {
            cursor = table[i];

            while (cursor != NULL)
            {
                tmp = cursor;
                cursor = cursor->next;
                free(tmp);
                count1++;
            }
        }
    }

    free(cursor);
    if (count1 == count)
        {
            return true;
        }

    return false;
}
...