Thread: Why is it the program responsibility to deallocate dynamic memory allocation?

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    Why is it the program responsibility to deallocate dynamic memory allocation?

    I hope this is the right place to ask this question, I just couldn't find any suitable place. I tried to answer this to myself but couldn't.

    Why isn't dynamic memory (allocated on the heap) automatically deallocated by the operating system?
    Why is it the process responsibility to do it?
    If a program does something stupid like that:

    Code:
    int main() {
         malloc(20);
         return 0;
    }
    These are 20 bytes that are not gonna be accessible by anyone, not even the parent-process that have created the process running this code.
    If it is a shared memory allocation, then there is a solid argument in favor of keeping it persist in memory even after the process creating it has ended, and you can in fact access it later through the OS API (the <sys/shm.h> library, if I remember correctly, provides this functionality), but I can't find any good reason for when it is not a shared memory. And it's not like the OS can't tell the difference between a shared memory allocation and a 'local' memory allocation - since its API provides different functions to do it.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Your sample program is a bad example, because when the program exits, the operating system reclaims any unshared memory allocated to it. The problem comes when the program continues, but does not free memory that it has allocated, but no longer uses.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Why isn't dynamic memory (allocated on the heap) automatically deallocated by the operating system?
    Where did you read that?
    It certainly isn't true of modern operating systems which place each process into a virtual address space.

    It might be true for archaic DOS, and some kinds of simple RTOS.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    228
    >Your sample program is a bad example, because when the program exits, the operating system reclaims any unshared memory allocated to it.

    >Where did you read that?

    Doesn't calling malloc without a corresponding free (on the pointer returned by malloc) cause a memory leak?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Absurd View Post
    >Where did you read that?

    Doesn't calling malloc without a corresponding free (on the pointer returned by malloc) cause a memory leak?
    Yes, but only within the context of the process that called malloc. When the program ends, that memory is automatically freed by the operating system.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    With a number of modern operating systems (windows, unix, etc) dynamically allocated memory IS released (or, more accurately, recovered) by the operating system as the program terminates.

    The catch is that the operating system has to be specifically designed so it can do that - that sort of thing is generally not something that hardware can do on its own (unless it is power cycled, and sometimes not even then). A number of older operating systems (including older variants of windows) were not designed with that in mind. Programs that run without an operating system also clearly cannot rely on the operating system cleaning up after them.

    The more common reason for the recommendation that programmers deallocate memory they allocate is to allow code reuse. One technique, for example, is to rename a program's main() function, recompile, and call that (renamed) function from other code. If the program being reused does not clean up properly after itself (i.e. it leaks memory) then it can compromise behaviour of the program in which it is reused - particularly if the new program calls the offending code repeatedly, or if it runs for a long time after calling the offending function. There is no way that an operating system can sensibly correct for that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    228
    Quote Originally Posted by Elkvis View Post
    Yes, but only within the context of the process that called malloc. When the program ends, that memory is automatically freed by the operating system.
    Wow, OK... because up until now, when I read 'memory leak' I understood that it means that the memory leaks after the program has terminated, not during its running time.
    Thanks.
    Last edited by Absurd; 09-25-2014 at 08:30 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also consider cleanup. In C, sometimes structs are used as "objects," meaning they're initialized and then destructed when freed by calling an initialize function and destruct function, respectively. If you just ignore destructing some constructed "object," then you might not clean up properly. So the advice is always clean up what you allocate even if it's just raw memory.
    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.

  9. #9
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Question from a beginner who just read about malloc() today:
    If we should free memory when using malloc(), shouldn't we free memory for normal variables when we are done using them?
    What I mean is for example:

    ...a lot of code here...
    a for loop using the variable i
    ... a lot of code here, not using the variable i.....

    Shouldn't we free the memory used by the variable i after the for loop here?
    It's no longer used so its' memory should be returned?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Stack variables are automatically freed (when the current scope or function ends). Memory allocated from malloc is not (hence the need for explicit free).
    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.

  11. #11
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Oh ok, thanks Elysia. But, memory allocated with malloc is also freed when the program ends (by the OS)?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but it is not freed while the program is running (unlike stack variables).
    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.

  13. #13
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    OK, thanks again

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I think that I should add that C is not an OS implementation, it is a language.

    Most of the C programs I write do not run on an operating system at all - There is NO operating system to free all the memory
    Fact - Beethoven wrote his first symphony in C

  15. #15
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    Quote Originally Posted by Click_here View Post
    I think that I should add that C is not an OS implementation, it is a language.

    Most of the C programs I write do not run on an operating system at all - There is NO operating system to free all the memory
    Could you elaborate on that statement? How does a C program run independent of an operating system unless the program is the operating system?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me.. (Dynamic memory allocation )
    By Devil211272 in forum C Programming
    Replies: 11
    Last Post: 10-17-2012, 09:10 AM
  2. Dynamic Memory Allocation
    By JamesD in forum C Programming
    Replies: 10
    Last Post: 03-12-2011, 12:08 AM
  3. allocation responsibility
    By ItayB in forum C Programming
    Replies: 21
    Last Post: 11-12-2010, 07:04 PM
  4. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  5. dynamic memory allocation - Please help
    By Space_Cowboy in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2002, 05:20 PM