By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

new or malloc()

P: n/a
which is better? "new" or the good old "malloc()". what is (if there is) the
diference?
--
--------------------------------
Krečimir ©pes
B.B. & Idol Graphics
http:// bb.gamer.hr
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"DigitalDragon" <kr***********@zg.hinet.hr> wrote in message
news:bh**********@ls219.htnet.hr...
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
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2

P: n/a


DigitalDragon wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3

P: n/a

Karl Heinz Buchegger wrote:

DigitalDragon wrote:

which is better? "new" or the good old "malloc()". what is (if there is) the
diference?
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.
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.