SIGSEGV (Segmentation fault)

Ловлю Segmentation faul в функции push() на строчке, heap->arr[heap->heapSize]->data = node.

Никак не могу понять понять в чем проблема, вроде никакого обращения за пределы выделенной памяти быть не может, просто передаю указатель. Подскажите, что не так.

Вот код полностью:

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

void countFrequency(FILE* in, int* tab) {
    int c;
    while ((c = getc(in)) != EOF) {
        tab[c]++;
    }
}

typedef struct Node {
    int count;
    int chr;
    struct Node* left;
    struct Node* right;
} Node;

Node* newNode(int p, int d, Node* l, Node* r) {
    Node* n = (struct Node*) malloc(sizeof(Node));
    n->count = p;
    n->chr = d;
    n->left = l;
    n->right = r;
    return n;
}

typedef struct qNode {
    Node* data;
    int priority;
} qNode;

typedef struct Heap {
    qNode** arr;
    int heapSize;
} Heap;

Heap* createHeap() {
    Heap* heap = (Heap*) malloc(sizeof(Heap));
    heap->heapSize = 0;
    heap->arr = (qNode**) malloc(sizeof(qNode) * 256);
    return heap;
}

void swap(qNode** a, qNode** b)
{
    qNode* t = *a;
    *a = *b;
    *b = t;
}

int getRight(int n) {
    if((((2 * n) + 1) < 256) && (n >= 1))
        return (2*n)+1;
    return -1;
}

int getLeft(int n) {
    if((( 2 * n) < 256) && (n >= 1))
        return 2*n;
    return -1;
}

int getParent(int n) {
    if ((n > 1) && (n < 256)) {
        return n/2;
    }
    return -1;
}

void heapify(Heap* heap, int n) {
    int l = getLeft(n);
    int r = getRight(n);

    int m = n;

    if ((l <= heap->heapSize) && (l>0)) {
        if (heap->arr[l]->priority < heap->arr[m]->priority) {
            m = l;
        }
    }

    if ((r <= heap->heapSize && (r>0))) {
        if (heap->arr[r]->priority < heap->arr[m]->priority) {
            m = r;
        }
    }

    if (m != n) {
        swap(&heap->arr[n], &heap->arr[m]);
        heapify(heap, m);
    }
}

qNode* pop(Heap* heap) {
    qNode* m = heap->arr[1];
    heap->arr[1] = heap->arr[heap->heapSize--];
    heapify(heap, 1);
    return m;
}

void push(Heap* heap, Node* node, int priority) {
    heap->arr[++heap->heapSize] = (qNode*) malloc(sizeof(qNode));
    heap->arr[heap->heapSize]->data = node;
    heap->arr[heap->heapSize]->priority = priority;
    int i = heap->heapSize;
    while((i>1) && (heap->arr[getParent(i)]->priority > heap->arr[i]->priority)) {
        swap(&heap->arr[i], &heap->arr[getParent(i)]);
        i = getParent(i);
    }
}

void padding ( char ch, int n ){
    int i;
    for ( i = 0; i < n; i++ )
        putchar ( ch );
}

void print ( Node *root, int level ){
    if ( root == NULL ) {
        padding ( '\t', level );
        puts ( "~" );
    } else {
        print ( root->right, level + 1 );
        padding ( '\t', level );
        printf ( "%c, %d\n", root->chr, root->count);
        print ( root->left, level + 1 );
    }
}

int main() {
    FILE* in = fopen("in.txt", "rb");

    int tab[256] = {0};
    countFrequency(in, tab);

    Node* nodes[256] = {0};

    int N = 0;
    for (int i = 0; i < 256; ++i) {
        if (tab[i]) {
            Node* node = newNode(tab[i], i, NULL, NULL);
            printf("nodes[%d]: count = %d, chr = %c;\n", N, tab[i], i);
            nodes[N++] = node;
        }
    }

    Heap* heap = createHeap();
    
    push(heap, nodes[0], nodes[0]->count);

    fclose(in);

    //print(nodes[0], 10);
}

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