Freeing memory

This is a discussion on Freeing memory within the C Programming forums, part of the General Programming Boards category; 1) When a program ends and you haven't freed you dynamically allocated memory, what are the consequenses? Does the program ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Freeing memory

    1) When a program ends and you haven't freed you dynamically allocated memory, what are the consequenses? Does the program free memory when main returns? Does it try to and hope for the best? Will it try to if it crashes?

    2) Another question about memory. How do languages that have a garbage collect, like Java, but don't let you free memory, manage memory? Do they wait for the object not to be used anymore, so no harm done? Is that possible? Do the use a more sophisticated way?
    Would there be a benefit of having the ability to manually free memory in languages like Java, or they do the work for you without disadvantages? If they have disadvantages what are those?

    Thanx!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    1) When a program ends and you haven't freed you dynamically allocated memory, what are the consequenses? Does the program free memory when main returns? Does it try to and hope for the best? Will it try to if it crashes?
    It depends on the platform, but typically the operating system will release the memory allocated for the process.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Quote Originally Posted by C_ntua View Post
    1) When a program ends and you haven't freed you dynamically allocated memory, what are the consequenses? Does the program free memory when main returns? Does it try to and hope for the best? Will it try to if it crashes?
    All memory for a program is freed* when one of the following happens:
    - The program exits.
    - The program crashes.
    You don't get any consequences in the OS for not doing so, but you may get consequences in your code for doing so.
    * This assumes we're talking about a modern OS.
    Is it a good idea to do so? Most on cboard considers it bad practice.
    For C, you can probably get away with it.
    In other languages, such as C++, it's even worse.
    In other words: don't rely on it. Free all your memory.

    2) Another question about memory. How do languages that have a garbage collect, like Java, but don't let you free memory, manage memory? Do they wait for the object not to be used anymore, so no harm done? Is that possible? Do the use a more sophisticated way?
    Usually, thread run a thread in the background that wakes after a certain period of time and starts checking for any unused memory and frees it.
    Some garbage collectors can work in other ways, such as checking and releasing memory after each allocation.

    Would there be a benefit of having the ability to manually free memory in languages like Java, or they do the work for you without disadvantages? If they have disadvantages what are those?
    The benefit would perhaps be lower memory consumption.
    The disadvantage would be that the garbage compiler would become more complex and you may not really use this feature as it (mostly) defies the use of a garbage compiler.

    Unlike C, many of today's languages (including C++) have ways to avoid having explicitly free memory you allocate.
    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.

  4. #4
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Usually, thread run a thread in the background that wakes after a certain period of time and starts checking for any unused memory and frees it.
    How about a garbage collector that freeing memory based on code-blocks (just like Pascal/Delphi)?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Oh, I'm sure there are plenty of methods for garbage collectors. I only know a few. Or maybe two. That I can think of, but there are definitely more.
    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.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    I usually initialize all my data structure pointers to null. Then if an error occurs I first print an error message, and then call a killall function that releases the memory. eg suppose there is a data structure called 'mesh'

    Code:
    typedef struct mesh
    {
      int ntri, int nvert;
      triangle *tri; /* triangle array */
      vector *vert;  /* vertex array */
    }mesh;
    
    mesh *m_ptr;

    Code:
    void killall()
    {
      killmesh();
      ...
      ...
    }
    
    void killmesh()
    {
       if (m_ptr != NULL)
       {
          if (m_ptr->vert != NULL)
          {
             free(m_ptr->vert);
          }
          if (m_ptr->tri != NULL)
         {
            free(m_ptr->tri);
         }
         free(m_ptr);
       }
    }

    I've heard that in situations like this if you directly free memory of m_ptr for eg. without freeing m_ptr->vert and m_ptr->tri, it can result in memory leaks.
    Last edited by broli86; 06-28-2008 at 11:58 AM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Quote Originally Posted by broli86 View Post
    I've heard that in situations like thsi if you directly free memory of m_ptr for eg. without freeing m_ptr->vert and m_ptr->tri, it can result in memory leaks.
    It does result in leaks. Freeing the base pointer does not automatically free the pointers inside the struct.
    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. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by Elysia View Post
    It does result in leaks. Freeing the base pointer does not automatically free the pointers inside the struct.
    Ok but then I guess you can't do anything about the ints which are inside the struct because they were not allocated by the user but by the OS.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    You only free what you allocate with malloc.
    The ints are freed by your free on the base pointer.
    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.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    More precisely I believe that you free everything that the pointer points (or the memory block it points to). The pointer itself is also freed by the base pointer

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    The pointer itself is stored on the stack so it cannot be freed.
    It simply frees the entire memory block that the pointer points to.
    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.

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Elysia View Post
    The pointer itself is stored on the stack so it cannot be freed.
    It simply frees the entire memory block that the pointer points to.
    Aha, didn't know that. So, since you mentioned it, is every pointer declared on the stack or the pointers that allocate memory? Or do I have it all wrong?

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    A pointer is a variable.
    Therefore it is stored on the stack.
    A pointer is a variable that stores an address.
    An address to the memory block allocated (in the case of malloc).
    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.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    It depends on the platform, but typically the operating system will release the memory allocated for the process.
    What about Shared Memory though? I think that would stay reserved wouldn't it?
    Using shmat(), shmget(), shmdt()... and all the Windows equivalents?

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    That would be unlikely wouldn't it?
    A process must own the shared memory, the creator. So if it dies, then it must disappear.
    Otherwise the OS would continue eating 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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory freeing function
    By johndoe in forum C Programming
    Replies: 4
    Last Post: 02-17-2006, 01:08 AM
  2. Freeing memory for non-pointer variables
    By SuperGodAntMan in forum C++ Programming
    Replies: 7
    Last Post: 02-11-2006, 12:30 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. memory allocation and freeing
    By Jase in forum Linux Programming
    Replies: 1
    Last Post: 05-25-2003, 06:26 AM
  5. freeing memory
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 04-27-2003, 08:51 PM

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