Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Rephrased introductory sentence and added a couple of conditions that can lead to malloc failure.

A program will crash if memory is requested while the heap is exhausted. This error can be generated due to Program that fail to check for and properly handle memory allocation failures will have undefined behavior and likely crash when heap space is exhausted. Heap exhaustion can arise as a result of the following possible reasons:

  • A memory leak
  • An infinite loop
  • The program requires more memory than is present by default in the heap
  • Incorrect implementation of common data structures (hash tables, vectors, etc.)
  • The total amount of system memory is exhausted, possibly due to some other processes
  • The maximum size of a process' data segment (set by setrlimit()) has been exceeded

If malloc() is unable to return the requested memory, it returns NULL instead.

...