нарушение доступа для чтения 0xFFFFFFFFFFFFFFFF

if (current->key == key) { - здесь не дает запуститься программе и пишет "Вызвано необработанное исключение: нарушение доступа для чтения. current было 0xFFFFFFFFFFFFFFFF." вот код

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

#define TABLE_SIZE 1000

struct Node {
    int key;
    struct Node* next;
};

struct Node* createNode(int key) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        perror("Memory allocation error");
        exit(EXIT_FAILURE);
    }
    newNode->key = key;
    newNode->next = NULL;
    return newNode;
}

int hashFunction(int key) {
    return key % TABLE_SIZE;
}

void addToHashTable(struct Node* hashTable[], int key) {
    int index = hashFunction(key);
    struct Node* newNode = createNode(key);
    newNode->next = hashTable[index];
    hashTable[index] = newNode;
}

int isInHashTable(struct Node* hashTable[], int key) {
    int index = hashFunction(key);
    struct Node* current = hashTable[index];
    while (current != NULL) {
        if (current->key == key) {
            return 1;
        }
        current = current->next;
    }
    return 0;
}
void freeHashTable(struct Node* hashTable[]) {
    for (int i = 0; i < TABLE_SIZE; ++i) {
        struct Node* current = hashTable[i];
        while (current != NULL) {
            struct Node* temp = current;
            current = current->next;
            free(temp);
        }
    }
}
void removeDuplicates(int A[], int n, int uniqueNumbers[], int* k) {
    struct Node* hashTable[TABLE_SIZE] = { NULL };
    *k = 0;

    for (int i = 0; i < n; ++i) {
        int key = A[i];
        if (key == 0 && !isInHashTable(hashTable, key)) {
            uniqueNumbers[(*k)++] = key;
            addToHashTable(hashTable, key);
        }
        else if (!isInHashTable(hashTable, key)) {
            uniqueNumbers[(*k)++] = key;
            addToHashTable(hashTable, key);
        }
    }

    freeHashTable(hashTable);
}

int main() {
    FILE* input = fopen("input.bin", "rb");
    FILE* output = fopen("output.bin", "wb");

    if (input == NULL || output == NULL) {
        perror("Error opening files");
        exit(EXIT_FAILURE);
    }

    int n;
    fread(&n, sizeof(int), 1, input);

    int* array = (int*)malloc(n * sizeof(int));
    if (array == NULL) {
        perror("Memory allocation error");
        exit(EXIT_FAILURE);
    }

    fread(array, sizeof(int), n, input);

    int* uniqueNumbers = (int*)malloc(n * sizeof(int));
    if (uniqueNumbers == NULL) {
        perror("Memory allocation error");
        exit(EXIT_FAILURE);
    }

    int k;

    removeDuplicates(array, n, uniqueNumbers, &k);

    fwrite(&k, sizeof(int), 1, output);
    fwrite(uniqueNumbers, sizeof(int), k, output);

    fclose(input);
    fclose(output);

    free(array);
    free(uniqueNumbers);
    

    return 0;
}

Ответы (0 шт):