C Dynamic Memory Allocation - Common Errors

Common Errors

The improper use of dynamic memory allocation can frequently be a source of bugs.

Most common errors are as follows:

  • Not checking for allocation failures. Memory allocation is not guaranteed to succeed. If there's no check for successful allocation implemented, this usually leads to a crash of the program or the entire system.
  • Memory leaks. Failure to deallocate memory using free leads to buildup of memory that is non-reusable memory, which is no longer used by the program. This wastes memory resources and can lead to allocation failures when these resources are exhausted.
  • Logical errors. All allocations must follow the same pattern: allocation using malloc, usage to store data, deallocation using free. Failures to adhere to this pattern, such as memory usage after a call to free or before a call to malloc, calling free twice ("double free"), etc., usually leads to a crash of the program.

Read more about this topic:  C Dynamic Memory Allocation

Famous quotes containing the words common and/or errors:

    How like a prodigal doth nature seem,
    When thou, for all thy gold, so common art!
    Thou teachest me to deem
    More sacredly of every human heart,
    Since each reflects in joy its scanty gleam
    Of Heaven, and could some wondrous secret show,
    Did we but pay the love we owe,
    And with a child’s undoubting wisdom look
    On all these living pages of God’s book.
    James Russell Lowell (1819–1891)

    The weak have one weapon: the errors of those who think they are strong.
    Georges Bidault (1899–1983)