6

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; 
}
4
  • 2
    malloc(sizeof(data*))? And what if sizeof(data) != sizeof(data*) (which seems quite likely considering your assignments later)? Commented Oct 18, 2017 at 6:01
  • why wouldn't it be? It is just a pointer to some place in memory. Data is a struct that is defined in the header file so I don't see how it can't be the size of data*
    – Ohunter
    Commented Oct 18, 2017 at 6:03
  • @Ohunter Size of a pointer is fixed. But that of your structure may be greater than the size of a pointer. Size of a pointer of any type is same.
    – J...S
    Commented Oct 18, 2017 at 6:05
  • The size of a pointer is typically 4 or 8 bytes (depending on if you're on a 32 or 64 bit system). Now considering that your structure itself seems to have at least four pointers in itself, that means the structure will be at least 16 or 32 bytes large. Now think about what happens when you allocate only 8 bytes for a structure containing (at least) 32 bytes. Commented Oct 18, 2017 at 6:07

2 Answers 2

10

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.

3
  • wouldn't sizeof(*D) be the same as sizeof(D) since *D is just a pointer to D?
    – Ohunter
    Commented Oct 18, 2017 at 6:05
  • @Ohunter *D is not a pointer at all. D is a pointer (to *D). And no, they're not the same size. On a 64-bit machine, D is 8 bytes (size of a pointer) whereas *D is 40 bytes (size of data: 4 pointers plus 1 val).
    – melpomene
    Commented Oct 18, 2017 at 6:10
  • I had the same issue (but in C++ and using LLVM). It took me hours to debug because the first allocation succeeds, but the error only shows in the next call to malloc (new operator in C++), and not when writing to unallocated memory. I was very much confused. One more C/C++ gotcha for me.
    – zazke
    Commented Sep 16, 2023 at 21:03
1

You are allocating memory for data pointer (data*), but you need memory to store data. You have to use D = malloc(sizeof(data)).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.