LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Memory leak: How risky not to free allocated memory. (http://www.linuxquestions.org/questions/linux-general-1/memory-leak-how-risky-not-to-free-allocated-memory-692627/)

kaz2100 12-23-2008 08:23 PM

Memory leak: How risky not to free allocated memory.
 
Hya,

background:
I am revising old program, (not coded by me) which does not compile without -fwritable-string. These days, gcc does not accept this option. (ANSI string is const, so cannot modify. With that option, gcc-3 used to allocate writable memory for string, so that can be overwritten. It is a violation of ANSI rulebook.)
Program is a small pipe. I have no idea how much memory is allocated.

So far, I have done:
I managed to allocate necessary memory for these variables, copy into and and program itself look good.

So far, I have not done:
However, modifications happened all over the place, and I have not figured out overall program structure. At this moment, none of allocated memory is freed. I know it is not good, but not easy to keep track.

Web search:
Web says that modern OS takes care of allocated memory requested by program when the program exits. Another Web says not freeing allocated memory is a good way to make memory leak.

Question:

If the former is correct, every time this program finishes, memory is freed, so my penguin stays healthy. If the latter, my penguin may get sick. Do I need to spend another three days to take care of allocated memory?

What is my best option?

I will appreciate suggestions.

Happy Penguins!

Valery Reznic 12-24-2008 12:00 AM

Quote:

Originally Posted by kaz2100 (Post 3385640)
Hya,

background:
I am revising old program, (not coded by me) which does not compile without -fwritable-string. These days, gcc does not accept this option. (ANSI string is const, so cannot modify. With that option, gcc-3 used to allocate writable memory for string, so that can be overwritten. It is a violation of ANSI rulebook.)
Program is a small pipe. I have no idea how much memory is allocated.

So far, I have done:
I managed to allocate necessary memory for these variables, copy into and and program itself look good.

So far, I have not done:
However, modifications happened all over the place, and I have not figured out overall program structure. At this moment, none of allocated memory is freed. I know it is not good, but not easy to keep track.

Web search:
Web says that modern OS takes care of allocated memory requested by program when the program exits. Another Web says not freeing allocated memory is a good way to make memory leak.

Question:

If the former is correct, every time this program finishes, memory is freed, so my penguin stays healthy. If the latter, my penguin may get sick. Do I need to spend another three days to take care of allocated memory?

What is my best option?

I will appreciate suggestions.

Happy Penguins!

Both WEB correct. If your program is "short living", i.e it's aloocate memory, do whatever it do and then exist, then it is no real harm in not freeing allocated memory - OS will take care. If on the other hand, program is long standing one, i.e it do same operation and each time allocate and never freed memory - it's a bad thing, and should be fixed


All times are GMT -5. The time now is 02:08 PM.