0

I have written a program for heap sort and I tried to have both the array and the size of the array in a single structure but there seems to be some error in my code (I think it's in the initializing part). Here is my code.

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

int cmp;

typedef struct heap
{
    int *arr, n;
}heap;

void main()
{
    heap *h;
    scanf("%d",&h->n);
    h->arr = (int*)malloc(h->n*sizeof(int));
    
    for(int i=0; i<h->n; i++)
        scanf("%d",h->arr+i);
    
    build_heap(h);
    heap_sort(h);
    
    for(int i=0; i<h->n; i++)
        printf("%d ",h->arr[i]);
    printf("\n%d\n",cmp);
    free(h->arr);

}

I compiled it in linux platform and the error I got was Segmentation fault (core dumped) Can someone explain why I got this error and give a possible solution to this.

1
  • 3
    Using h->n and h->arr directly after declaring h as an uninitialised heap * cannot be correct. Instead, declare h as a heap and use h.n and h.arr. Remember that h->n is nothing more than a synonym for (*h).n, and using the * operator on an uninitialised pointer will result in a segmentation fault at best. Commented Oct 12, 2020 at 15:11

3 Answers 3

3

By doing this heap *h; you have declared a pointer to a heap struct, but what does this pointer actually point to at this point?

You only need to allocate the memory for your heap:

void main()
{
    heap *h;
    // Alloc and initialize the pointer to the heap
    h = malloc(sizeof(heap));
    scanf("%d",&h->n);
    h->arr = malloc(h->n*sizeof(int));
    
    for(int i=0; i<h->n; i++)
        scanf("%d",h->arr+i);
    
    build_heap(h);
    heap_sort(h);
    
    for(int i=0; i<h->n; i++)
        printf("%d ",h->arr[i]);
    printf("\n%d\n",cmp);
    free(h->arr);

    // Don't forget to free the heap as well!
    free(h);

}

Also, it is a good practice to not cast the return value of malloc.

Sign up to request clarification or add additional context in comments.

Comments

1

Do it a bit different way:

typedef struct heap
{
    size_t n;
    int arr[]; 
}heap;

only one malloc and free is needed.

    size_t size;
    heap *h;

    if(scanf("%zu", &size) != 1) { /* error handling*/};
    h = malloc(sizeof(*h) + size * sizeof(h -> arr[0]));
    if(!h) { /* error handling*/};
    h -> size = size;

    /*...... */


    free(h);

Comments

1

When you declare heap *h; you declare a pointer, but after you never initialize h to point anywhere.

You probably want this:

int main()
{
  heap h;                              // now h is a heap and no more a pointer to heap
  scanf("%d", &h.n);
  h.arr = malloc(h.n * sizeof(int));   // the cast to (int*) is useless

  for (int i = 0; i < h.n; i++)
    scanf("%d", h.arr + i);            //BTW: &h.arr[i] would be more readable

  build_heap(&h);
  heap_sort(&h);

  for (int i = 0; i < h.n; i++)
    printf("%d ", h.arr[i]);

  printf("\n%d\n", cmp);
  free(h.arr);
}

There may be more problems in build_heap and in build_heap, but I can't tell you more as you didn't show these functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.