Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hy to everyone. For a couple of hours now, I've tried to implement an array of linked lists. I would like to implement K-Way-Sort in C, but it seems I can't figure out why I don't make a proper memory allocation.

What's even more peculiar is that the program will work for an initial value < 3. Higher than that and I will receive an Access violation writing location 0x0000000000000000. in Microsoft Visual Studio or a Segmentation Fault in CodeBlocks(with gcc compiler - MinGW). All my intuition says I am initializing the wrong way some pointers, but I also now that without a NULL init, the pointer would simply point to the default value, that being 0. Though because of that, I've checked and double checked the source of some init's and I start to think that I don't have a proper understanding about the underlying principle.

Below I've added part of the code.

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
typedef int Date;
typedef struct Element {
    Date valoare;
    struct Element* urmator;
} Element;

/* Working with the heap */
// getting the parent
int parinte(int i) {
    return (int)(i / 2);
}

// the left child
int stanga(int i) {
    return 2*i;
}

// the right child
int dreapta(int i) {
    return 2*i + 1;
}

// Redoing the heap structure
void minHeapify(Element ***a, int i, int size) {
    int l, r, minim;
    l = stanga(i);
    r = dreapta(i);
    if (l<size && (**a)[l].valoare < (**a)[i].valoare) {
        minim = l;
    }
    else minim = i; 

    if (r < size && (**a)[l].valoare < (**a)[minim].valoare) {
        minim = r;
    }

    if (minim != i) {
        Element *temp;
        temp = *a[i];
        *a[i] = *a[minim];
        *a[minim] = temp;
        minHeapify(a, minim, size);
    }
}

void Afisare(Element* cap) {
    if (cap == NULL)
        printf("Hopa mitica! \n");
    while (cap != NULL) {
        printf("Valoare: %d\n", cap->valoare);
        cap = cap->urmator;
    }
}

void constructieMinHeap(Element ***array, int heapSize) {
    int i, j;
    for (i = (int)(heapSize / 2); i >= 0; i--)
        minHeapify(array, i, heapSize);

    for (j = 0; j<heapSize; j++) {
        Afisare(*array[j]);
    }
}

// Add a node at the beginning of the list
void InserareInceput(Element** cap, Date val) {
    Element *elem;
    elem = (Element*)malloc(sizeof(Element));
    elem->valoare = val;
    elem->urmator = *cap;
    *cap = elem;
}

void creare_k_liste(Element ***array, int nrListe) {
    int j;
    *array = (Element**)malloc(nrListe * sizeof(Element*));
    for (j = 0; j <= nrListe; j++) {
        // The Exception is thrown right here
        *array[j] = NULL;
    }
    for (j = 0; j<nrListe; j++) {
        int nrElem, i, elem;
        printf("Numarul de elemente din lista[%d]: ", j);
        scanf("%d", &nrElem);
        for (i = 0; i<nrElem; i++) {
            printf("El[%d]=", i);
            scanf("%d", &elem);
            InserareInceput(array[j], elem);
        }
    }
    for (j = 0; j<nrListe; j++) {
        Afisare(*array[j]);
    }
}

int main() {
    // declarare lista vida
    Element **array = NULL;
    int nrListe;

    printf("Cate liste vrei?"); 
    scanf("%d", &nrListe); // the number of lists that I want
    creare_k_liste(&array, nrListe); // create nr_liste lists

    // construct a minHeap using the first element from each linked list
    constructieMinHeap(&array, nrListe);

    return 0;
}

Any help, tips or ideas would be greatly appreaciated.

share|improve this question

put on hold as off-topic by Mat's Mug 2 days ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Mat's Mug
If this question can be reworded to fit the rules in the help center, please edit the question.

    
"Where's my bug?" questions don't belong on this site. Reproduce the issue with a Minimal, Complete, Verifiable Example (MCVE) and then you'll have Stack Overflow material. – Mat's Mug 2 days ago