free memory?

B

bingo

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
 
A

acehreli

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
 
S

Stephen Horne

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

bingo

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

James Kanze

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?
 
S

Salt_Peter

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

Default User

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

I would expect many would mention it. The C++ Programming Language
devotes about 2/3 of a page.




Brian
 
P

Pranav

C memory allocating functions only allocate memory.,
In C++ we have user defined data type called Objects which need to be
created, just by allocating some memory to the required amount of
objects is not sufficient. CPP objects are created by Constructors and
which need to be called while object creation(they initialize member
objects also). The dynamically initialized objects which gets created
by using constructors in the dynamically allocated memory. These
objects need to be deleted properly by deallocating memory. Objects
destroyed by calling destructors. So when you call malloc and free
they just allocate and free requested no of bytes of memory(remember
malloc returns a generic pointer to the requested no of bytes we need
explicitly typecast and divide the chunk into objects). Now when you
call free it just free's the memory chunk without calling destructor
of the object. So the Objects are not destroyed properly. For this
reason it is always better to use new and delete for user defined
object creation on heap.
 
J

James Kanze

I would expect many would mention it. The C++ Programming
Language devotes about 2/3 of a page.

The C++ Programming Language is a reference, as well as a
tutorial. I also think that Stroustrup was writing with people
who already know C in mind.

I should have phrased that: what introductory tutorial is he
reading which mentions malloc and free. (From the tone of the
posting, however, I simply assumed that any book he'd be reading
would be an introductory tutorial.)
 
S

Stephen Horne

And when those conditions are met, a smart pointer is nearly always a
better solution.

Yes and no. There seem to be a million definitions of how smart (and
in what way) *a* smart pointer should be.

I've reached the point where I make it a personal rule when I'm
thinking of writing a smart pointer to count slowly to 10 - wasting
easily enough time to write the damn thing - and then take a very deep
breath. I stripped out loads a while back because I could never
remember the precise rules for any one of them - when the author can't
read his own code, no-one else has a chance.

*The* smart pointer (as in std::auto_ptr) is certainly a good idea
though, particularly in those cases where you wouldn't use a pointer
except that you have constructor-calling issues.
 
S

Stephen Horne

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

He mentioned numerical recipes - presumably the classic "Numerical
Recipes in C" - not a C++ book at all.
 
S

Stephen Horne

Should we then presume the OP isn't reading any C++ book at all? I
would think that it could be seen as implied in my inquiry.

I just noticed the clue elsewhere in the thread - beyond that, I know
nothing.
 
J

James Kanze

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.

That depends. The most frequent direct use of dynamic memory
(at the application level) is for objects with explicit
lifetimes, and smart pointers aren't generally appropriate for
those. (At the lower level, of course, things like std::vector
will make extensive use of dynamic memory. And I don't think
that smart pointers are necessarily appropriate there either.)
 

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,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top