Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: malloc vs new

Reply
Thread Tools

Re: malloc vs new

 
 
Jonathan Bartlett
Guest
Posts: n/a
 
      01-19-2005
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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-19-2005

"Jonathan Bartlett" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      01-19-2005
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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-19-2005
Jonathan Bartlett <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      01-19-2005
On Wed, 19 Jan 2005 17:12:01 GMT, Mike Wahler
<(E-Mail Removed)> wrote:

>
> "Jonathan Bartlett" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-19-2005
CBFalconer <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-19-2005
"Chris Croughton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-25-2005

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jonathan Bartlett <(E-Mail Removed)> 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 ?



 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      01-25-2005
On Tue, 25 Jan 2005 11:08:00 -0500, Bill Cunningham
<(E-Mail Removed)> wrote:

>
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Jonathan Bartlett <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-25-2005
Chris Croughton <(E-Mail Removed)> writes:
> On Tue, 25 Jan 2005 11:08:00 -0500, Bill Cunningham
> <(E-Mail Removed)> wrote:
>> "Keith Thompson" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Jonathan Bartlett <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments