new or malloc()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DigitalDragon

    new or malloc()

    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


  • Jakob Bieling

    #2
    Re: new or malloc()

    "DigitalDra gon" <kresimir.spes@ zg.hinet.hr> wrote in message
    news:bh07db$9cg $1@ls219.htnet. hr...[color=blue]
    > which is better? "new" or the good old "malloc()". what is (if there is)[/color]
    the[color=blue]
    > diference?[/color]


    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)


    Comment

    • Karl Heinz Buchegger

      #3
      Re: new or malloc()



      DigitalDragon wrote:[color=blue]
      >
      > which is better? "new" or the good old "malloc()". what is (if there is) the
      > diference?[/color]

      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
      kbuchegg@gascad .at

      Comment

      • Alexander Terekhov

        #4
        Re: new or malloc()


        Karl Heinz Buchegger wrote:[color=blue]
        >
        > DigitalDragon wrote:[color=green]
        > >
        > > which is better? "new" or the good old "malloc()". what is (if there is) the
        > > diference?[/color]
        >
        > malloc allocates storage[/color]

        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();
        [color=blue]
        > new allocates storage and constructs an object in that storage[/color]

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

        regards,
        alexander.

        Comment

        Working...