Search code examples
cpointersstructmalloc

What is the meaning of `malloc(): corrupted top size`


I am trying to append a struct pointer to a dynamically allocated array. After that I return the struct pointer that was added to the array. This is then dereferenced and put into a variable that I want to print out. My problem is that when I try to access a value (i.e. struct.id) the compiler tells me malloc(): corrupted top size and Aborted (core dumped).

Everywhere I have looked so far simply answered how to fix the problem, but I can't seem to figure out what it actually means or just find very vague definitions that don't help me. Also none of the solutions work for my specific condition.Aborted (core dumped)


Solution

  • What is the meaning of malloc(): corrupted top size

    It means that your program has invoked Undefined Behaviour most likely by accessing the allocated memory block outside its bounds.

    But it can also be caused by something else (for example unrelated memory access changing the memory containing malloced memory metadata).

    Also none of the solutions work for my specific condition.Aborted (core dumped)

    It is one of the ways UB can express itself. It is not "your condition". You need to find what is invoking the UB.