Search code examples
cdynamic-allocation

weird SIGABORT on malloc/calloc call


running my program with gdb I get this:

fem.o: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.

Program received signal SIGABRT, Aborted. 0xb7fe1424 in __kernel_vsyscall ()

I found that this error arises after this code:

problem->y0 = (double *)calloc(n_tot, sizeof(double));

problem is a structure which has double *y0 as member.

Previously in the function, I do this

problem = (fem_problem *)calloc(1, sizeof(fem_problem));

and I don't get any error neither problem == NULL.

some suggestion?

ADD:

I already checked the content of n_tot, it has the right number


Solution

  • There could be any number of problems, for example:

    • The value of n_tot could be garbage.

    • You have written outside of an allocated block, and in doing so you have destroyed data structures used to maintain the heap.