[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

free memory?

bingo

10/13/2008 11:41:00 PM

Hi, All:
I'm new to C++ and get a little confused about the way to free
memory.
It seems to me that both delete and free can do the job, then
what's the difference between them?
Is delete used to free the memory allocated from new, and free to
free the memory allocated by malloc?

Thanks a lot.
Bin

19 Answers

Victor Bazarov

10/14/2008 12:20:00 AM

0

bingo wrote:
> I'm new to C++ and get a little confused about the way to free
> memory.
> It seems to me that both delete and free can do the job, then
> what's the difference between them?
> Is delete used to free the memory allocated from new, and free to
> free the memory allocated by malloc?

What book on C++ are you reading that does not explain the importance of
'new' and 'delete' as opposed to 'malloc' and 'free'?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

acehreli

10/14/2008 1:16:00 AM

0

On Oct 13, 4:40 pm, bingo <zhn...@gmail.com> wrote:
> Hi, All:
>    I'm new to C++ and get a little confused about the way to free
> memory.

Memory usually hosts objects; so it is about destroying the objects as
well.

>    It seems to me that both delete and free can do the job, then
> what's the difference between them?

delete destroys the object by calling its destructor first, then
releases the memory.

>    Is delete used to free the memory allocated from new, and free to
> free the memory allocated by malloc?

new not only allocates memory, but constructs an object on it as well;
so, yes, 'delete' what is 'new'ed.

Ali

Daniel T.

10/14/2008 1:27:00 AM

0

bingo <zhngbn@gmail.com> wrote:

> I'm new to C++ and get a little confused about the way to free
> memory.
> It seems to me that both delete and free can do the job, then
> what's the difference between them?

delete will call the destructor(s) on the objects being released. free
exist purely for backward compatibility reasons.

> Is delete used to free the memory allocated from new, and free to
> free the memory allocated by malloc?

Yes. To the above add; use 'new' and 'delete' rather than 'malloc' and
'free' when writing C++.

Stephen Horne

10/14/2008 5:02:00 AM

0

On Mon, 13 Oct 2008 16:40:37 -0700 (PDT), bingo <zhngbn@gmail.com>
wrote:

>Hi, All:
> I'm new to C++ and get a little confused about the way to free
>memory.
> It seems to me that both delete and free can do the job, then
>what's the difference between them?
> Is delete used to free the memory allocated from new, and free to
>free the memory allocated by malloc?

malloc and free are really just a part of the C heritage. Normally,
you should use new and delete.

As others have said, new and delete also ensure proper initialisation
and cleanup - constructor and destructor calls etc. However, even for
Plain Old Data (POD), you should never free memory that was allocated
with new, and you should never delete memory that was allocated with
malloc.

The reason is that new and delete may use different pools of memory to
malloc and free, meaning that mixing your calls can potentially cause
memory corruption on the heap (or rather heaps).

In practice, new will normally use the same heap as malloc - but not
always. For example, when using multithreading, sometimes each thread
will have its own heap to speed up memory management. In situations
like this, you need to write code that's as "normal" as possible
(unless there are specific rules to the contrary) or else you risk
falling into all kinds of holes. Freeing your memory in an unexpected
way definitely qualifies. Depending on how your runtimes are
implemented, it could easily bypass a mechanism that associates that
memory with the heap it came from.

bingo

10/14/2008 5:40:00 AM

0

On Oct 13, 10:01 pm, Stephen Horne <sh006d3...@blueyonder.co.uk>
wrote:
> On Mon, 13 Oct 2008 16:40:37 -0700 (PDT), bingo <zhn...@gmail.com>
> wrote:
>
> >Hi, All:
> >   I'm new to C++ and get a little confused about the way to free
> >memory.
> >   It seems to me that both delete and free can do the job, then
> >what's the difference between them?
> >   Is delete used to free the memory allocated from new, and free to
> >free the memory allocated by malloc?
>
> malloc and free are really just a part of the C heritage. Normally,
> you should use new and delete.
>
> As others have said, new and delete also ensure proper initialisation
> and cleanup - constructor and destructor calls etc. However, even for
> Plain Old Data (POD), you should never free memory that was allocated
> with new, and you should never delete memory that was allocated with
> malloc.
>
> The reason is that new and delete may use different pools of memory to
> malloc and free, meaning that mixing your calls can potentially cause
> memory corruption on the heap (or rather heaps).
>
> In practice, new will normally use the same heap as malloc - but not
> always. For example, when using multithreading, sometimes each thread
> will have its own heap to speed up memory management. In situations
> like this, you need to write code that's as "normal" as possible
> (unless there are specific rules to the contrary) or else you risk
> falling into all kinds of holes. Freeing your memory in an unexpected
> way definitely qualifies. Depending on how your runtimes are
> implemented, it could easily bypass a mechanism that associates that
> memory with the heap it came from.

Thanks a lot. You guys are really helpful.
The reason I'm sticking with malloc and free is that I was using the
codes from Numerical Recipe, which are written is C.

NewsFan

10/14/2008 7:59:00 AM

0

Yes, maybe different HEAP.

Thanks.

Ian Collins

10/14/2008 8:11:00 AM

0

NewsFan wrote:

What are you replying to?

> Yes, maybe different HEAP.

What heap?

--
Ian Collins

James Kanze

10/14/2008 9:26:00 AM

0

On Oct 14, 2:20 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> bingo wrote:
> > I'm new to C++ and get a little confused about the way to free
> > memory.
> > It seems to me that both delete and free can do the job, then
> > what's the difference between them?
> > Is delete used to free the memory allocated from new, and free to
> > free the memory allocated by malloc?

> What book on C++ are you reading that does not explain the
> importance of 'new' and 'delete' as opposed to 'malloc' and
> 'free'?

What C++ book is he reading that even mentions malloc and free?

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Daniel T.

10/14/2008 9:44:00 AM

0

bingo <zhngbn@gmail.com> wrote:
> On Oct 13, 10:01 pm, Stephen Horne <sh006d3...@blueyonder.co.uk>
> wrote:
> > On Mon, 13 Oct 2008 16:40:37 -0700 (PDT), bingo <zhn...@gmail.com>
> > wrote:
> >
> > >Hi, All:
> > >   I'm new to C++ and get a little confused about the way to free
> > >memory.
> > >   It seems to me that both delete and free can do the job, then
> > >what's the difference between them?
> > >   Is delete used to free the memory allocated from new, and free to
> > >free the memory allocated by malloc?
> >
> > malloc and free are really just a part of the C heritage. Normally,
> > you should use new and delete.
> >
> > As others have said, new and delete also ensure proper initialisation
> > and cleanup - constructor and destructor calls etc. However, even for
> > Plain Old Data (POD), you should never free memory that was allocated
> > with new, and you should never delete memory that was allocated with
> > malloc.
> >
> > The reason is that new and delete may use different pools of memory to
> > malloc and free, meaning that mixing your calls can potentially cause
> > memory corruption on the heap (or rather heaps).
> >
> > In practice, new will normally use the same heap as malloc - but not
> > always. For example, when using multithreading, sometimes each thread
> > will have its own heap to speed up memory management. In situations
> > like this, you need to write code that's as "normal" as possible
> > (unless there are specific rules to the contrary) or else you risk
> > falling into all kinds of holes. Freeing your memory in an unexpected
> > way definitely qualifies. Depending on how your runtimes are
> > implemented, it could easily bypass a mechanism that associates that
> > memory with the heap it came from.
>
> Thanks a lot. You guys are really helpful.
> The reason I'm sticking with malloc and free is that I was using the
> codes from Numerical Recipe, which are written is C.

That's not a valid reason.

You should, quite simply, never write "malloc" in your C++ code, unless
you are passing the block to code already written that will "free" it.

Salt_Peter

10/14/2008 12:35:00 PM

0

On Oct 13, 7:40 pm, bingo <zhn...@gmail.com> wrote:
> Hi, All:
> I'm new to C++ and get a little confused about the way to free
> memory.
> It seems to me that both delete and free can do the job, then
> what's the difference between them?
> Is delete used to free the memory allocated from new, and free to
> free the memory allocated by malloc?
>
> Thanks a lot.
> Bin

The best way to deallocate memory... is not to allocate it on the heap
at all.
Using new/delete is frowned upon unless a set of conditions warrants
its use.
And when those conditions are met, a smart pointer is nearly always a
better solution.