Memory Functions - Still Learning

This is a discussion on Memory Functions - Still Learning within the C Programming forums, part of the General Programming Boards category; and the OS does that because it is an interrupt? whereas if the program completes (and I don't free memory), ...

  1. #46
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    and the OS does that because it is an interrupt?

    whereas if the program completes (and I don't free memory), the OS won't free it for me? thus leading to a memory leak?

    PS. actually thinking about it (should have thought first!), the memory leak occurs while the program is running...

    thanks

  2. #47
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That is typically when memory leaks occur... unless you are running Windows. In which case memory leaks are part of the operational process. In all seriousness though, you should be aware that typically the OS will just return all memory that a process was using back to the system once execution of that process has ceased. So memory leaks are usually irrelevant once the program has closed. The exceptions to the rules are OS specific and not worth mentioning at this point.

  3. #48
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, the problem with memory leaks in general is that the APPLICATION, if it runs for long enough, will use up all memory (or much more than it should), which will cause problems where the system runs slowly, and perhaps also cause the application to crash if it doesn't handle running out of memory well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #49
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    ok, so just to clarify (so I never need ask again!).

    if the program is interrupted, the OS will free up any memory that has been allocated.

    if the program exits (finishes, crashes), the OS will free up any memory that has been allocated.

    if the program runs for 100 days and each 24 hours you malloc some memory, but don't free that memory once you've finished with it, a memory leak is the result and your memory runs out and the system grinds to a halt.

    by 'free up' I mean is marked as available to other processes.

    I am on unix only, had my fill of MS. Incidentally, I used to do MS support on Exchange. I have troubleshooted many a memory leak in the MTA (emsmta.exe). Good to understand the causes, etc.

    thanks for your patience, explanations...

  5. #50
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I hate to use absolutes since I have made programs that do cause problems that the OS was completely unable to acknowledge or cope with on its own (which is good, since it helps you know how to reproduce/avoid these situations).

    A good general rule of thumb is that anything that application does, is undone as soon as the program is interrupted. File handles are eliminated. Memory handles are returned back to the OS. Sockets are invalidated. In theory the OS returns to the state it would be in had your program not run at all. In practice that isn't always the case. But that is a whole nother thread.

  6. #51
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    dougieb, essentially, yes.

    The OS manages your process, and its going away no matter how it went. By its own volition or by force.

    I have a quibble with your phrase "...but don't free that memory once you've finished with it, a memory leak is the result and ..."

    A memory leak is what is happening slowly, a dribble at a time. It's not an instantaneous end result. Hence they may go undetected in many programs. Even commercial ones. They terminate normally before all memory is exhausted. Lucky for them. Saving face of their creators. But not to their credit.

    It would be nice if the operating system told you "hey by the way, that recent program that just finished? it left crappola all over the place, had open files, allocated crud, and was ugly". It could avert unexpected headaches down the road during Christmas season when transactions are at their peak, if you're in such an environment. The worst time for things to blow up.

  7. #52
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by nonoob View Post
    It would be nice if the operating system told you "hey by the way, that recent program that just finished? it left crappola all over the place, had open files, allocated crud, and was ugly". It could avert unexpected headaches down the road during Christmas season when transactions are at their peak, if you're in such an environment. The worst time for things to blow up.
    To some degree, that's true.
    There are methods for findings leaks and so, thankfully. And there are lots of "verifier" programs out there that make sure you don't leak memory or handles, etc.

    Oh and, just because the OS will clean everything up for you doesn't mean it's good practice to ignore closing handles and freeing memory. Some tend to skip doing that when the application is closing down and having the OS clean it up.
    Many consider it bad practice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #53
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    that's great guys, makes sense.

    thanks,

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Reallocating Memory
    By Booie2k1 in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 07:09 AM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 04:53 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21