Thread: free memory in structure

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    free memory in structure

    I have a structure which contains multiple dynamic allocated memory.

    If I free one of its dynamic allocated memory, can the freed memory be used? Or only when I free the whole structure then the freed memory can be used?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    used by whom?
    if you free memory it cannot be used by your application.
    but because it is returned to the pool of the free memory it can be use by the memory menager during next allocations.
    The memory manager does not know where do you strore the pointer to the allocated memory - inside the structure or in any simple variable...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    used by the memory manager. an example
    Code:
    struct opStructure {
    
    int **array1;
    int **array2;
    }
    when I do this "delete [] array1[i][j]" or "delete [] array1[i]", the memory used by the application doesn't drop.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so you are talking about returning memory to the operating system?
    It is very OS specific and has no direct connection with new/delete

    In most cases the already requested from the OS memory stays marked as "used by the application" so next new just uses this memory without requiring new page from the OS
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If anything, new and delete are used in classes. When I studyed C at college a while back, all memory allocations were defined with malloc(). But I do not know if the same rule still applys. All I know for sure is that C++ uses new and delete over malloc()
    Last edited by swgh; 01-06-2007 at 06:31 AM.
    Double Helix STL

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    malloc(), calloc(), realloc(), and free() manage raw dynamic memory in C. operators new and delete, among other things, manage raw dynamic memory in C++.

    Assuming the application is running on an operating system, dynamic memory (in fact, all memory resources) will ultimately be obtained from the operating system. However, this is no guarantee that free() or operator delete will actually return memory to the operating system. Instead, as vart said, it may simply mark the memory so it can be used again by the operating system --- so the next call to malloc() [in C] or operator new (in C++) may simply reuse memory rather than having to ask the operating system for it. The reason for this is that the act of obtaining memory from the operating system is relatively expensive, so these sorts of tricks are a performance optimisation. The actual techniques used depend on the compiler, the implementation of its library, and the operating system.

    But the net effect is that, if you are monitoring memory usage by your program, neither calling free() or invoking operator delete are guaranteed to immediately result in the application using less memory. However, if you watch, you will often find that subsequent usage of malloc() or operator new() will not necessarily result in the application using more memory.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    thanks for the advises guys!

    I suspect my application sometimes run out of address space and I will have error when using using new []. Any suggestions or advice how to detect the address space is running out?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    when new fails it throws an exception
    use try/catch to determine it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    yes, new will throw an exception, but I'm thinking of am I able to detect it before reaching the exception? So that I can free some memory before the exception is called
    Last edited by franziss; 01-06-2007 at 10:07 AM.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    no I don't think so...
    but in the exceptio handler you can do that
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should catch the exception, free some memory, and try again.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    ok, so i should write the exception for the new[] on my own source code?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i should write the exception for the new[] on my own source code?
    Do you mean throw or catch the exception? Are you familiar with exceptions?

    Handling memory errors is tricky. If you have a certain part of the code where you think you will run out of memory and you can free memory if you need to, then simply create a loop or a couple of if blocks that try a couple times to get memory. The first attempt should be in a try/catch block. If it fails, then free some memory and try again. The second attempt should also probably be in a try/catch block so you can exit the program gracefully.

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    nope, i'm not familiar with exceptions, I thought it is more common in Java, but i roughly get what you mean =)

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You should read about try/catch blocks
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. help understanding free and memory leaks
    By mc61 in forum C Programming
    Replies: 4
    Last Post: 04-08-2008, 12:47 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. Memory leak with detached pthreads - how to free?
    By rfk in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2007, 06:50 AM
  5. OS out of memory if malloc without free?
    By hkuser2001 in forum C Programming
    Replies: 7
    Last Post: 04-24-2006, 07:23 AM