Reputation: 117
I have this piece of code but I get Abort(core dumped). When I comment the Destroy line everything is okay so I suppose that the mistake is there. Any ideas?
#include <stdio.h>
#include <stdlib.h>
#define maxelem 100
#define NIL -1
typedef int BHItem;
struct node {
BHItem data;
int priority;
};
typedef struct node *BHNode;
BHNode BHCreate() //This function creates an empty heap
{
BHNode heap;
int i;
heap=malloc(maxelem*sizeof(struct node));
for (i=0; i<maxelem; i++) {
heap[i].data=NIL;
heap[i].priority=NIL;
}
}
void BHDestroy(BHNode heap) //This function destroys a heap
{
free(heap);
}
int main()
{
BHNode heap;
heap=BHCreate();
BHDestroy(heap); //Destroy the heap
return 0;
}
Upvotes: 0
Views: 660
Reputation: 31296
The problem is that BHCreate
is missing a return heap;
as a final statement. It should look like this:
BHNode BHCreate()
{
BHNode heap;
int i;
heap=malloc(maxelem*sizeof(struct node));
for (i=0; i<maxelem; i++) {
heap[i].data=NIL;
heap[i].priority=NIL;
}
return heap;
}
You should turn on compiler warnings to discover such things:
$ gcc main.c -Wall -Wextra
main.c: In function ‘BHCreate’:
main.c:26:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
Upvotes: 4