main: malloc.c:2405: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize – 1)) == 0)' failed.
When trying to produce a doubly linked list where the user can only add to the head or tail, I end up getting the lovely abortion message at the top.
I have used gdb to narrow it down to what function it is that is causing this, but I really don't know what I'm doing wrong. It allocates the first node, then after that, it throws me the error.
The function is as follows:
data* initD(){
data *D = NULL;
D = malloc(sizeof(data*));
if (!D){
printf("Error in allocation of data\n");
exit(0);
D->head = NULL;
D->tail = NULL;
D->next = NULL;
D->prev = NULL;
D->val = -1;
return D;
You're writing outside of your allocated buffer.
That's because your malloc call passes the wrong size:
D = malloc(sizeof(data*)); // allocate memory for a single pointer
should be:
D = malloc(sizeof(data)); // allocate memory for a whole struct
But really,
D = malloc(sizeof *D); // allocate memory for whatever D points to
is best because then you don't need to see the declaration of D
to check that this line is correct.