1

I am using Visual C++ 2010 for a C project. I have the following code in main():

ARRAY2D a;

arr2_init(&a, 5, 5);  /* 5x5 dynamic, multi-demensional array (of type int) */
arr2_release(&a); 

I'm not sure if I need the last line. Can I omit out arr2_release() at the end of the program in modern OS's? I'm using Windows 7.

Phil
  • 607
  • 1
  • 8
  • 22
  • It is good practice to release the allocated memory. If you run the script enough and you don't release it, it could become a memory hog and cause your system to run slowly. Especially when working with C, which has a high standard for memory management – Adjit Apr 24 '14 at 16:14
  • so far i know there is no guarantee that OS will imminently reclaim the memory you want to leak..... so its better you release them. – sunny1304 Apr 24 '14 at 16:16
  • 2
    I am confused as to why anyone would ask this question. Is there some *cost* associated with writing code the right way? Is there some *benefit* to cutting corners needlessly? Write code using the best possible practices all the time; violate those practices only when you have an amazingly compelling reason. – Eric Lippert Apr 24 '14 at 16:16
  • Note that the linked post about automatically freeing says that the memory is automatically freed in most modern OSes *after the program terminates*. While you're program is *running* you can still have a memory leak if you don't explicitly free any memory that you have allocated. The OS won't help you there. C does not support automatic garbage collection. – lurker Apr 24 '14 at 16:18
  • Okay, thanks. Yeah, I was still confused from an earlier discussion. It makes sense to free it instead of pure hope the OS will save you. – Phil Apr 24 '14 at 16:20
  • Even if someone else would clean up after you, you should clean up after yourself its just good manners ;) – Casper Beyer Apr 24 '14 at 16:23
  • @EricLippert: It would be a beter candidate for that one, yes. It's just that someone would have to go in there and change the C++-only code to C-also code first. Also, yo do not talk about other resources. – Deduplicator Apr 24 '14 at 16:44

2 Answers2

1

Yes, you can avoid releasing any resource manually which the runtime or the OS will clean up after you.
Still, do not do so please.

It is a valid optimisation for faster shutdown (and sometimes even for faster execution in exchange for memory consumption), though you must be picky about which resources you leave around:

  • Memory and file descriptors are efficiently handled by the OS (ancient platforms not doing so have mostly succumbed to disuse. Still, there are a few tiny systems not able to free this).
  • FILE buffers are efficiently cleaned up by the runtime.
  • Windows GUI resources are not efficiently cleaned up this way, it needs longer.

Anyway, do the cleanup and develop the right mind-set, it makes searching for leaks much easier and is better transferable to bigger and longer-running tasks.
Premature optimisation is the root of all evil. (The expert only option to optimise after measurement and careful consideration does not apply yet)

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
0

Always free your memory. The operating system will release a process' resources when it terminates, which includes its memory. But that doesn't give you garbage collection (you have to use different languages for that). Also note that it only does so after your program has ended (also stated in the comments), so as long as your program is running, the memory will not be freed if you don't do it.

Your code might be used as part of a bigger program someday, even if it's now only a few lines. So always make sure to release all resources you acquire. Also, as a C programmer, thinking about resource management should be a habit anyway.

glglgl
  • 89,107
  • 13
  • 149
  • 217
lethal-guitar
  • 4,438
  • 1
  • 20
  • 40