new or malloc()

D

DigitalDragon

which is better? "new" or the good old "malloc()". what is (if there is) the
diference?
 
J

Jakob Bieling

DigitalDragon said:
which is better? "new" or the good old "malloc()". what is (if there is) the
diference?


Use new and new[]. Difference is, new is type safe and calls
constructors for classes/structs, malloc is/does none of that.

hth
 
K

Karl Heinz Buchegger

DigitalDragon said:
which is better? "new" or the good old "malloc()". what is (if there is) the
diference?

malloc allocates storage
new allocates storage and constructs an object in that storage

So the question: 'which one is better?' makes no sense - they
do different things.
 
A

Alexander Terekhov

Karl said:
malloc allocates storage

and doesn't throw an exception. As for "new", it's all in the
declarations (placement and array functions aside for a moment):

void * operator new(std::size_t size) throw(std::bad_alloc);
void * operator new(std::size_t size, const std::nothrow_t &) throw();
new allocates storage and constructs an object in that storage

Nah, new-expression is hardly related to malloc(). ;-)

regards,
alexander.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top