Vijay said:
Hi All,
I am learning C++ and have one question.
Using free or delete we can release the memory. After releasing memory
where this
released memory will go.. Does it go to back operating system?
The standard does not make any promisses about the effects of delete other
than that it calls a deallocation function to deallocate the memory. What
happens is not further specified. This leaves considerable freedom to the
implementation.
As for free(), it is defined in the C standard. All that is guaranteed is
that the memory becomes available for further allocation. However, even
that is not to be taken seriously: implementations often take the license
to return the memory to the operating system, in which case the memory may
be acquired by other processes so that it is not available anymore.
Some implementations use mixed strategies, e.g., they keep a pool of memory
around from which allocations of small objects are served whereas big
objects are served via system calls to the underlying OS.
For good code, do not rely on assumptions about what new/delete or
malloc/free will actually do. Treat them as abstract programming primitives
for memory management.
Best
Kai-Uwe Bux
ps.: If you are learning C++, I would recommend that you focus on the use of
std::string and standard containers and iterators first. This will, for
good measure, reduce your needs to manage memory manually. Come back to
memory management once you learned about writing exception safe code. Many
traps and difficulties with memory management are seriously aggravated by
exceptions -- since every new must match with one and only one delete along
every possible path of flow control, the diversions of flow control induced
by exceptions make life harder. Explicit memory management in C++ is
difficult and you should not needlessly start out with one of the hardest
aspects of C++ programming: you are make the learning curve unnecessarily
steep.