Is it a memory leak in C?

2019-09-26 12:22发布

问题:

An answer by user:surendra nath on this post stated that the code posted by OP has a memory leak, whereas, most of the time I've come across that a leak occurs when we fail to free a dynamically allocated memory region but I couldn't see any dynamic allocation in OPs code. He quoted this wiki definition, here.

And another definition of Memory Leak from user:artificial idiot to post was:

Subtle definition: Failure to release reachable memory which is no longer needed for your program to function correctly. This is nearly impossible to detect with automated tools or by programmers who are not familiar with the code. While technically it is not a leak, it has the same implications as the naive one. This is not my own idea only. You can come across projects that are written in a garbage collected language but still mention fixing memory leaks in their changelogs.

So, my question:

  • Is the point stated by "surendra nath" on OPs code, can be said as a memory leak considering the definition of a memory leak given by "artificial idiot" or wikipedia? And if so, then why?
  • Does the memory leak in C always refer to the failure to free a previously dynamically allocated memory?

P.S. - I don't know if the definitions from wiki & "artificial idiot" have the same meaning, since wiki definitions are sometimes too broad to understand.

回答1:

The issue that artificial idiot raises in The Best Memory Leak Definition can sometimes be a concern. Suppose you have a program that does something like this:

  1. Construct an array of 1 million dynamically-allocated objects.
  2. Do something with that array.
  3. Construct another array of 1 million dynamically-allocated objects (with no references to anything in the first array).
  4. Do something with that array.
  5. Free the second array.
  6. Free the first array.

During steps 3-5, the first array is still using memory, even though none of its data is needed. As a result, the program as a whole uses twice as much total memory as it really needs. It should free the first array after step 2 if it no longer needs it -- then its memory could be reused for the second array.

This is the kind of memory leak that can occur even in garbage-collected languages -- if the variable holding the reference to the first array doesn't get reassigned or go out of scope, it will keep all that data from being GC'ed.

A sufficiently clever optimizer could theoretically address this automatically. If it can analyze the control and data flow, and determine that the first array is never used after step 2, it could reorder step 6 to that place. But in many cases it may be difficult for the compiler to determine this.

Writing modular code will often avoid problems like this. If you create separate functions for processing each of the arrays, the functions should allocate them, use them, and free them.