Regarding released memory.

V

Vijay

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?
Please explain.

Thanks in advance.
Vijay
 
R

Rolf Magnus

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?
Please explain.

That's specific to the implementation. It might go back or it might not.
Sometimes, it is only returned in blocks, so it will only go back when a
whole block has been freed.
 
K

Kai-Uwe Bux

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.
 
V

Vijay

Kai-Uwe Bux said:
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.

Thanks for so valuable information.

One other doubt.

I have read in one e-book that new is allocating memory from 'free
storage' and malloc is allocating from heap.

Is it right?
If yes, then what is difference between free storage and heap?
Regards,
Vijay
 
K

Kai-Uwe Bux

Vijay wrote:
[snip]
One other doubt.

I have read in one e-book that new is allocating memory from 'free
storage' and malloc is allocating from heap.

Is it right?

No -- or, well, it is a rather meaningless statement: the C++ language does
not have a memory model distinguishing "free storage", "heap", or "stack".
As far as the programmer is concerned, memory is handled behind the scenes
by compiler magic. All we know about memory is
* that it consists of units that a capable of storing an unsigned char,
* that we can take the address of any object (because an object *is* a
region in memory although it might not need to be contiguous),
* that we can explicitly acquire and release memory by new and delete,
and probably a few more minor things that just escape me right now.

How the resource memory is handled by the implementation (e.g., whether it
comes segmented with a distinguished stack segment) is not specified by the
C++ standard.


Best

Kai-Uwe Bux
 
G

Greg

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?
Please explain.

No, I'm fairly sure that the released memory doesn't go anywhere but
instead stays put inside the computer.

Greg
 
R

Rolf Magnus

Greg said:
No, I'm fairly sure that the released memory doesn't go anywhere but
instead stays put inside the computer.

Where is that computer's operating sytem running, if not inside it?
 
Z

Zara

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?

If there was a home for lost memories, then realeased memory returns
home after retiring.

But, what about virtual memory? It softly and suddnely vanishes away.

For the Snark was a boojum, you see
Please explain.

"Please explain" Is it not the typical ending of homework assignment?
Please explain.


Bets regards
 
V

Vijay

Zara said:
If there was a home for lost memories, then realeased memory returns
home after retiring.

But, what about virtual memory? It softly and suddnely vanishes away.

For the Snark was a boojum, you see


"Please explain" Is it not the typical ending of homework assignment?
Please explain.


Bets regards
haha.. Good question by Zara
Would like to inform Zara that have completed my class room study 2
year back.
Regards,
Vijay
 
J

Joe Van Dyk

Kai-Uwe Bux said:
Vijay wrote:




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.

By implementation, do you generally mean "compilier"?

Joe
 
B

Bo Persson

Joe Van Dyk said:
By implementation, do you generally mean "compilier"?

An implementation consists of some sort of a translator and a standard
library.

The standard document is deliberately vague on exactly how an
implementation supplies the required functionality. It doesn't even
say that you need to have a compiler! It could be an interpreter, or
something else (don't know what).


Bo Persson
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Kai-Uwe Bux wrote:

One other doubt.

I have read in one e-book that new is allocating memory from 'free
storage' and malloc is allocating from heap.

Is it right?
If yes, then what is difference between free storage and heap?

As others have pointed out C++ does not deal with those things, however
on all implementations that I have knowledge of "free storage" is the
same as the heap.

If you didn't know already I would also like to point out that it's
generally not a good idea to mix usage of new and malloc in one application.

Erik Wikström
 
A

Andre Kostur

If you didn't know already I would also like to point out that it's
generally not a good idea to mix usage of new and malloc in one
application.

Why not? The two calls perform different logical functions. new brings
into existance fully-formed objects (granted, a fully-formed char is
rather simple....), where malloc (and friends) creates blocks of
unstructured memory. Now you do have to be careful to delete what you new,
and free what you malloc (and do not delete what you malloc and/or free
what you new).
 
G

Guest

Why not? The two calls perform different logical functions. new brings
into existance fully-formed objects (granted, a fully-formed char is
rather simple....), where malloc (and friends) creates blocks of
unstructured memory. Now you do have to be careful to delete what you new,
and free what you malloc (and do not delete what you malloc and/or free
what you new).

First of all I said generally, there are situations where one might have
to use both. But, once again, generally there is nothing you can do with
malloc that you can't use new for. In my opinion new adds a layer of
abstraction and I see no reason to use malloc if you don't have to work
under that level of abstraction for other reasons.

Erik Wikström
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top