Re: malloc vs new

Discussion in 'C Programming' started by Jonathan Bartlett, Jan 19, 2005.

  1. puzzlecracker wrote:
    > Why is "new" a C++ language-level construct while "malloc" is a
    > library function?
    >


    Because "new" does a whole lot more than malloc. new not only allocates
    memory, it initializes it based on the type of data requested. New also
    determines the amount of data it needs for you while for malloc() you
    have to calculate it.

    For those of you flaming the question-asker, on what newsgroup _would_
    this be an appropriate question? I know of know language-comparison
    newsgroups. Might as well pick one of the language newsgroups and ask
    it there.

    Jon
    ----
    Learn to program using Linux assembly language
    http://www.cafeshops.com/bartlettpublish.8640017
     
    Jonathan Bartlett, Jan 19, 2005
    #1
    1. Advertising

  2. Jonathan Bartlett

    Mike Wahler Guest

    "Jonathan Bartlett" <> wrote in message
    news:...
    > puzzlecracker wrote:
    > > Why is "new" a C++ language-level construct while "malloc" is a
    > > library function?
    > >

    >
    > Because "new" does a whole lot more than malloc. new not only allocates
    > memory, it initializes it based on the type of data requested. New also
    > determines the amount of data it needs for you while for malloc() you
    > have to calculate it.
    >
    > For those of you flaming the question-asker, on what newsgroup _would_
    > this be an appropriate question? I know of know language-comparison
    > newsgroups. Might as well pick one of the language newsgroups and ask
    > it there.


    I've already heard the 'excuse' "but there isn't a newsgroup
    for my question", when someone posts OT. Even if their claim
    is true, it certainly doesn't justify an OT post.

    -Mike
     
    Mike Wahler, Jan 19, 2005
    #2
    1. Advertising

  3. Jonathan Bartlett

    CBFalconer Guest

    Jonathan Bartlett wrote:
    > puzzlecracker wrote:
    >
    >> Why is "new" a C++ language-level construct while "malloc" is a
    >> library function?

    >
    > Because "new" does a whole lot more than malloc. new not only
    > allocates memory, it initializes it based on the type of data
    > requested. New also determines the amount of data it needs for
    > you while for malloc() you have to calculate it.


    Ignoring the initialization part:

    #define new(x) (x = malloc(sizeof *x))

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Jan 19, 2005
    #3
  4. Jonathan Bartlett <> writes:
    > puzzlecracker wrote:
    >> Why is "new" a C++ language-level construct while "malloc" is a
    >> library function?
    >>

    >
    > Because "new" does a whole lot more than malloc. new not only
    > allocates memory, it initializes it based on the type of data
    > requested. New also determines the amount of data it needs for you
    > while for malloc() you have to calculate it.
    >
    > For those of you flaming the question-asker, on what newsgroup _would_
    > this be an appropriate question? I know of know language-comparison
    > newsgroups. Might as well pick one of the language newsgroups and ask
    > it there.


    comp.lang.c++ is the appropriate newsgroup, since C++ has both "new"
    and "malloc".

    Note that, the way the question is phrased, it's asking about "new"
    *given* the fact that "malloc" is a library function.

    An appropriate comp.lang.c question might be:

    Why is "malloc" a library function rather than a language-level
    construct like C++'s "new"?

    There's no single clear answer to that question. Historical reasons
    have a lot to do with it. If C had a language-level memory allocator,
    invoking it would almost certainly cause a function call to be
    generated. Most C constructs (other than explicit function calls, of
    course) tend to be compiled directly to code rather than implemented
    as calls. That's not a strict rule, but I'm sure it's what the
    original designers had in mind.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 19, 2005
    #4
  5. On Wed, 19 Jan 2005 17:12:01 GMT, Mike Wahler
    <> wrote:

    >
    > "Jonathan Bartlett" <> wrote in message
    > news:...
    >> puzzlecracker wrote:
    >> > Why is "new" a C++ language-level construct while "malloc" is a
    >> > library function?

    >>
    >> Because "new" does a whole lot more than malloc. new not only allocates
    >> memory, it initializes it based on the type of data requested. New also
    >> determines the amount of data it needs for you while for malloc() you
    >> have to calculate it.
    >>
    >> For those of you flaming the question-asker, on what newsgroup _would_
    >> this be an appropriate question? I know of know language-comparison
    >> newsgroups. Might as well pick one of the language newsgroups and ask
    >> it there.

    >
    > I've already heard the 'excuse' "but there isn't a newsgroup
    > for my question", when someone posts OT. Even if their claim
    > is true, it certainly doesn't justify an OT post.


    comp.lang.misc and comp.misc are groups designed for anything off-topic
    in other comp.lang.* and comp.* newsgroups. But the OP's question was
    on-topic in comp.lang.c++ anyway, since both the new operator and the
    malloc function are part of that language...

    Chris C
     
    Chris Croughton, Jan 19, 2005
    #5
  6. CBFalconer <> writes:
    > Jonathan Bartlett wrote:
    >> puzzlecracker wrote:
    >>
    >>> Why is "new" a C++ language-level construct while "malloc" is a
    >>> library function?

    >>
    >> Because "new" does a whole lot more than malloc. new not only
    >> allocates memory, it initializes it based on the type of data
    >> requested. New also determines the amount of data it needs for
    >> you while for malloc() you have to calculate it.

    >
    > Ignoring the initialization part:
    >
    > #define new(x) (x = malloc(sizeof *x))


    You'd probably also want:

    #define new_array(x, count) ((x) = malloc((count) * sizeof *(x)))

    <OT>
    Note that the usage is different from C++'s new operator.
    </OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 19, 2005
    #6
  7. Jonathan Bartlett

    Mike Wahler Guest

    "Chris Croughton" <> wrote in message
    news:...
    >
    > comp.lang.misc and comp.misc are groups designed for anything off-topic
    > in other comp.lang.* and comp.* newsgroups. But the OP's question was
    > on-topic in comp.lang.c++ anyway, since both the new operator and the
    > malloc function are part of that language...


    But this is comp.lang.c last time I checked. :)

    -Mike
     
    Mike Wahler, Jan 19, 2005
    #7
  8. "Keith Thompson" <> wrote in message
    news:...
    > Jonathan Bartlett <> writes:
    > > puzzlecracker wrote:
    > >> Why is "new" a C++ language-level construct while "malloc" is a
    > >> library function?
    > >>

    > >
    > > Because "new" does a whole lot more than malloc. new not only
    > > allocates memory, it initializes it based on the type of data
    > > requested. New also determines the amount of data it needs for you
    > > while for malloc() you have to calculate it.
    > >
    > > For those of you flaming the question-asker, on what newsgroup _would_
    > > this be an appropriate question? I know of know language-comparison
    > > newsgroups


    Maybe comp.programming ?
     
    Bill Cunningham, Jan 25, 2005
    #8
  9. On Tue, 25 Jan 2005 11:08:00 -0500, Bill Cunningham
    <> wrote:

    >
    > "Keith Thompson" <> wrote in message
    > news:...
    >> Jonathan Bartlett <> writes:
    >> > puzzlecracker wrote:
    >> >> Why is "new" a C++ language-level construct while "malloc" is a
    >> >> library function?
    >> >>
    >> >
    >> > Because "new" does a whole lot more than malloc. new not only
    >> > allocates memory, it initializes it based on the type of data
    >> > requested. New also determines the amount of data it needs for you
    >> > while for malloc() you have to calculate it.
    >> >
    >> > For those of you flaming the question-asker, on what newsgroup _would_
    >> > this be an appropriate question? I know of know language-comparison
    >> > newsgroups

    >
    > Maybe comp.programming ?


    comp.lang.misc would be my choice (everything about computing languages
    not covered by a more specific group). Or in an extreme case comp.misc.

    Chris C
     
    Chris Croughton, Jan 25, 2005
    #9
  10. Chris Croughton <> writes:
    > On Tue, 25 Jan 2005 11:08:00 -0500, Bill Cunningham
    > <> wrote:
    >> "Keith Thompson" <> wrote in message
    >> news:...
    >>> Jonathan Bartlett <> writes:
    >>> > puzzlecracker wrote:
    >>> >> Why is "new" a C++ language-level construct while "malloc" is a
    >>> >> library function?
    >>> >
    >>> > Because "new" does a whole lot more than malloc. new not only
    >>> > allocates memory, it initializes it based on the type of data
    >>> > requested. New also determines the amount of data it needs for you
    >>> > while for malloc() you have to calculate it.
    >>> >
    >>> > For those of you flaming the question-asker, on what newsgroup _would_
    >>> > this be an appropriate question? I know of know language-comparison
    >>> > newsgroups

    >>
    >> Maybe comp.programming ?

    >
    > comp.lang.misc would be my choice (everything about computing languages
    > not covered by a more specific group). Or in an extreme case comp.misc.


    comp.lang.misc is a good choice for most cross-language issues, but in
    this particular case, since C++ supports both malloc() and new,
    comp.lang.c++ would be appropriate.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 25, 2005
    #10
  11. Hello,
    I don't think whether this is to be posted here or not. Anyway, I
    know a little about both the languages so I think I can give me
    answer(Opinion might be a correct word) about that.
    'new' is treated as an operator in C++. You can even override that.
    And it is done like that because it has to do lot of other work other
    than initializing the memory. new operator has to call the constructors
    of a Class. And if it were treated as a function, it cannot take care
    of different types passed to that. And more over, in C++ if a pointer
    is initialized as int n = new int[10]; you can happily use that n as
    normal array, like n[0]. n[1].... I don't know if that is possible with
    C.(SuperProgrammers enlight me on this issue). I think I answered OR
    atleast gave you one reason.
     
    Hariprasad Govardhanam, Jan 25, 2005
    #11
  12. Jonathan Bartlett

    Old Wolf Guest

    Hariprasad Govardhanam wrote:

    > I don't think whether this is to be posted here or not. Anyway,
    > I know a little about both the languages so I think I can give
    > me answer(Opinion might be a correct word) about that.


    This should be in c.l.c++ (followups set, I hope), however to stop
    the flow of mis-information:

    > 'new' is treated as an operator in C++. You can even
    > override that. And it is done like that because it has
    > to do lot of other work other than initializing the memory.
    > new operator has to call the constructors of a Class.
    > And if it were treated as a function, it cannot take care
    > of different types passed to that.


    In C++ there is 'the new expression' and 'the new operator'.
    'the new operator' is essentially identical to malloc(), except
    that it is called 'operator new' instead. For example:

    .. int *ptr = (int *)operator new(10 * sizeof *ptr);

    'the new expression' does this sequence of events:
    .. calls 'operator new'
    .. calls constructor(s)
    .. "returns" a pointer of the correct type

    > And more over, in C++ if a pointer is initialized as
    > int n = new int[10];


    Error, I suppose you meant: int *n = new int[10];

    > you can happily use that n as normal array, like n[0]. n[1]....
    > I don't know if that is possible with C.


    .. int *n = calloc(10, sizeof *n);
    Read up the comp.lang.c FAQ section on pointers and arrays.
     
    Old Wolf, Jan 25, 2005
    #12
  13. Jonathan Bartlett

    Guest

    Keith Thompson wrote:
    > Chris Croughton <> writes:
    > > On Tue, 25 Jan 2005 11:08:00 -0500, Bill Cunningham
    > > <> wrote:
    > >> "Keith Thompson" <> wrote in message
    > >> news:...
    > >>> Jonathan Bartlett <> writes:
    > >>> > puzzlecracker wrote:

    (...)
    > >>> >
    > >>> > For those of you flaming the question-asker, on what newsgroup
    > >>> > _would_ this be an appropriate question? I know of know
    > >>> > language-comparison newsgroups
    > >>
    > >> Maybe comp.programming ?

    > >
    > > comp.lang.misc would be my choice (everything about computing
    > > languages not covered by a more specific group). Or in an extreme
    > > case comp.misc.

    >
    > comp.lang.misc is a good choice for most cross-language issues, but

    in
    > this particular case, since C++ supports both malloc() and new,
    > comp.lang.c++ would be appropriate.
    >


    Ironically, a group that strives to improve how C and C++ is taught, is
    one that deals with /both/ languagages:

    alt.comp.lang.learn.c-c++

    To the group's credit, folks there are more forgiving when it comes to
    enforcing topicality (newbies are simply redirected to read the FAQ!).
    - Anand

    --
     
    , Jan 25, 2005
    #13
  14. Oops, That was a mistake. I am really sorry about that.
    I was intending to say
    int*n = new int[10];
     
    Hariprasad Govardhanam, Jan 27, 2005
    #14
  15. Jonathan Bartlett

    Richard Bos Guest

    "Hariprasad Govardhanam" <> wrote:

    > Oops, That was a mistake. I am really sorry about that.


    Yes, and this is another. Or rather, two. Mistake number one is using
    Google Broken Beta to post without forcing it to provide proper
    quotations. Mistake number one b is doing so where your post makes no
    sense in the context of the thread - perhaps one more error in Google is
    that it makes it too easy to post to the wrong sub-thread? In any case,
    learn to make Google DTRT, or get a real newsreader.

    > I was intending to say
    > int*n = new int[10];


    Mistake number two is that this is still a syntax error in C. C++ is
    off-topic here.

    Richard
     
    Richard Bos, Jan 28, 2005
    #15
    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:
    710
  2. ravi
    Replies:
    0
    Views:
    457
  3. Peter
    Replies:
    34
    Views:
    1,969
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

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

    to malloc or not to malloc??

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

Share This Page