new or malloc()

Discussion in 'C++' started by DigitalDragon, Aug 8, 2003.

  1. 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
     
    DigitalDragon, Aug 8, 2003
    #1
    1. Advertising

  2. "DigitalDragon" <> wrote in message
    news:bh07db$9cg$...
    > 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)
     
    Jakob Bieling, Aug 8, 2003
    #2
    1. Advertising

  3. 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
     
    Karl Heinz Buchegger, Aug 8, 2003
    #3
  4. 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.
     
    Alexander Terekhov, Aug 8, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    734
  2. ravi
    Replies:
    0
    Views:
    481
  3. Peter
    Replies:
    34
    Views:
    2,058
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    503
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    343
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page