Heap Corruption?

Hi. I've been reading about various signal errors, and heap corruptions. I'd like to know what the various types of heap corruptions are (like I've seen detected double free mentioned), and if there are any good sources for learning these. In addition, in the case of a SIGABRT or other heap corruption, how could we detect its type to aid in debugging? Thank you!
Some library implementations have a heap_check() function that you can call at strategic points in your program.

A common source of heap corruption is storing into an array on the heap with an invalid index.
1
2
3
  int * arr[100];
  arr[-1] = 0;  // heap corruption
  arr[100] = 0;  // heap corruption Elements are 0-99 



> various types of heap corruptions

These are some of the common reasons for heap corruption:
. attempt to free memory that was already freed.
. attempt to free memory using a pointer that was not returned by an allocation function.
. attempt to access (read or write) memory that was freed earlier.
. attempt to write to memory using an uninitialized pointer
. attempt to access elements which are out of the bounds of a dynamically allocated array


> how could we detect its type to aid in debugging?

Let the standard library handle memory; std::vector<>, std::string etc. know how to manage memory correctly. This would eliminate most of the errors.

Use a tool, for example
valgrind on unix and unix-like systems: https://www.valgrind.org/docs/manual/quick-start.html
AppVerif on windows https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/application-verifier



Thank you so much. Those tools are extremely interesting and seem very useful. Thank you for sharing!
Topic archived. No new replies allowed.