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?
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?
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
used by the memory manager. an example
when I do this "delete [] array1[i][j]" or "delete [] array1[i]", the memory used by the application doesn't drop.Code:struct opStructure { int **array1; int **array2; }
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
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
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.
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?
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
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.
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
You should catch the exception, free some memory, and try again.
ok, so i should write the exception for the new[] on my own source code?
>> 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.
nope, i'm not familiar with exceptions, I thought it is more common in Java, but i roughly get what you mean =)
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