free memory in structure

This is a discussion on free memory in structure within the C++ Programming forums, part of the General Programming Boards category; I have a structure which contains multiple dynamic allocated memory. If I free one of its dynamic allocated memory, can ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    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.
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    when new fails it throws an exception
    use try/catch to determine it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    no I don't think so...
    but in the exceptio handler you can do that
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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,344
    >> 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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You should read about try/catch blocks
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
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, 05:04 AM
  2. help understanding free and memory leaks
    By mc61 in forum C Programming
    Replies: 4
    Last Post: 04-08-2008, 01:47 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  4. Memory leak with detached pthreads - how to free?
    By rfk in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2007, 07:50 AM
  5. OS out of memory if malloc without free?
    By hkuser2001 in forum C Programming
    Replies: 7
    Last Post: 04-24-2006, 08:23 AM

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