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

What is the difference between new() and malloc()?

P: n/a
RS
Hi,

What is the difference between new() and malloc()?

RS
Jul 23 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
RS wrote:
What is the difference between new() and malloc()?


'new' is the C++ way to create object in free store.
'mallow' is the C way to allocate some memory.
Jul 23 '05 #2

P: n/a
That's explained by lots of books. You can even google it.
Simply put, new is OOP, while malloc is not.

"RS" <rs@nospam.com> дʼ news:76SOd.358225$Xk.263642@pd7tw3no...
Hi,

What is the difference between new() and malloc()?

RS

Jul 23 '05 #3

P: n/a
Victor Bazarov wrote:
RS wrote:
What is the difference between new() and malloc()?


'new' is the C++ way to create object in free store.
'mallow' is the C way to allocate some memory.


You need to add malloc to your spell checker dictionary.
Jul 23 '05 #4

P: n/a
E. Robert Tisdale wrote:
Victor Bazarov wrote:
RS wrote:
What is the difference between new() and malloc()?

'new' is the C++ way to create object in free store.
'mallow' is the C way to allocate some memory.

You need to add malloc to your spell checker dictionary.


No, I need to work shorter days. So do you, apparently.
Jul 23 '05 #5

P: n/a
In article <37*************@individual.net>,
MatrixV <tr******@kcollege.com> wrote:
That's explained by lots of books. You can even google it.
Simply put, new is OOP, while malloc is not.


That's odd; I use malloc for OOP quite regularly.

Mind you, I do OOP in C quite regularly (for assorted reasons, most
of them actually reasonable), and I never use malloc when I'm actually
writing C++ code, whether I'm doing OOP or not...
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
My personal best estimate is that 90% of existing C code is crap (F various VO
crap), and 90% of existing NotC code is crap too. (I expect this to be true of
non-existing code as well.) --Dimitri Mazuik in the scary devil monastery
Jul 23 '05 #6

P: n/a
Dave Vandervies wrote:
[...] I never use malloc when I'm actually
writing C++ code, whether I'm doing OOP or not...


Really? Why? What's so bad about 'malloc'? It is
often _the_ function to call if you overload the
operator new...
Jul 23 '05 #7

P: n/a
In article <EO*******************@newsread1.mlpsca01.us.to.ve rio.net>,
Victor Bazarov <v.********@comAcast.net> wrote:
Dave Vandervies wrote:
[...] I never use malloc when I'm actually
writing C++ code, whether I'm doing OOP or not...


Really? Why? What's so bad about 'malloc'? It is
often _the_ function to call if you overload the
operator new...


Usually, if I find myself wanting to overload the operator new, I soon
find myself deciding that the code is better written in C than in C++
anyways.

When I choose C++ over C, the reasons usually involve things like
letting the compiler handle resource management by running constructors
and destructors without me having to write the code to do it myself,
so using malloc instead of new kind of defeats the purpose.

(This probably has at least as much to do with the type of code I write
as with the characteristics of the two languages.)
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
My personal best estimate is that 90% of existing C code is crap (F various VO
crap), and 90% of existing NotC code is crap too. (I expect this to be true of
non-existing code as well.) --Dimitri Mazuik in the scary devil monastery
Jul 23 '05 #8

P: n/a
"MatrixV" <tr******@kcollege.com> wrote in message news:<37*************@individual.net>...
That's explained by lots of books. You can even google it.
Simply put, new is OOP, while malloc is not.

"RS" <rs@nospam.com> дʼ news:76SOd.358225$Xk.263642@pd7tw3no...
Hi,

What is the difference between new() and malloc()?

RS

Hi,
using malloc following things are not possible,
-- calling constructor after allocating memory(new calls
constructor as well)
-- Type safety: (new returns a pointer of the right type)
-- Overridability: (new is an operator that can be overridden)

Thanks,

Basavaraj Kirunge
Jul 23 '05 #9

P: n/a
You though you are doing quite well, but you are not. As Mr. Kirunge said,
malloc can't do many things, it just alloc a memory space. Of course you can
implement new by malloc, and in fact that's the way it goes.
I suggest you to google it instead of asking here. I read a lot on this and
it's hard to put them all here.

"Dave Vandervies" <dj******@csclub.uwaterloo.ca> ????
news:cu**********@rumours.uwaterloo.ca...
In article <37*************@individual.net>,
MatrixV <tr******@kcollege.com> wrote:
That's explained by lots of books. You can even google it.
Simply put, new is OOP, while malloc is not.
That's odd; I use malloc for OOP quite regularly.

Mind you, I do OOP in C quite regularly (for assorted reasons, most
of them actually reasonable), and I never use malloc when I'm actually
writing C++ code, whether I'm doing OOP or not...
dave

--
Dave Vandervies

dj******@csclub.uwaterloo.ca My personal best estimate is that 90% of existing C code is crap (F various VO crap), and 90% of existing NotC code is crap too. (I expect this to be true of non-existing code as well.) --Dimitri Mazuik in the scary devil

monastery
Jul 23 '05 #10

P: n/a

MatrixV wrote:
You though you are doing quite well, but you are not. As Mr. Kirunge said, malloc can't do many things, it just alloc a memory space. Of course you can implement new by malloc, and in fact that's the way it goes.
I suggest you to google it instead of asking here. I read a lot on this and it's hard to put them all here.

"Dave Vandervies" <dj******@csclub.uwaterloo.ca> ????
news:cu**********@rumours.uwaterloo.ca...

Please don't top-post. See the FAQ if you need further explanation.

Brian

Jul 23 '05 #11

P: n/a
Victor Bazarov wrote:
RS wrote:
What is the difference between new() and malloc()?

'new' is the C++ way to create object in free store.
'mallow' is the C way to allocate some memory.


Mallow is a plant of the genus malva.
:-)
Jul 23 '05 #12

P: n/a
Victor Bazarov wrote:
Dave Vandervies wrote:
[...] I never use malloc when I'm actually
writing C++ code, whether I'm doing OOP or not...

Really? Why? What's so bad about 'malloc'? It is
often _the_ function to call if you overload the
operator new...


Malloc doesn't create objects. It allocated bytes.
new creates objects.
Jul 23 '05 #13

P: n/a
Basavaraj K wrote:
-- calling constructor after allocating memory(new calls
constructor as well)
The C++ memory allocation function (operator new) can be used to obtain
memory without constuction of an object.
-- Type safety: (new returns a pointer of the right type)
Which with the exception for the dastardly stupid case of POD types, is
initialize.
-- Overridability: (new is an operator that can be overridden)

WRONG! The operator can NOT be overriden. Only the memory allocator
can be overriden. It's a confusion to programmers that the unfrotunate
naming of the allocator implies that it is overriding the opreator.
Jul 23 '05 #14

P: n/a
"Ron Natalie" <ro*@sensor.com> wrote...
Victor Bazarov wrote:
Dave Vandervies wrote:
[...] I never use malloc when I'm actually
writing C++ code, whether I'm doing OOP or not...

Really? Why? What's so bad about 'malloc'? It is
often _the_ function to call if you overload the
operator new...


Malloc doesn't create objects. It allocated bytes.
new creates objects.


No!... Really? I learn something new every day. So,
when I overload operator new, I need to call another
operator new, is that what you're saying?
Jul 23 '05 #15

P: n/a
RS wrote:
Hi,

What is the difference between new() and malloc()?

RS


The difference is, malloc() is a function from the C library, while new
is a C++ operator. In fact, there is the operator called 'new' and
there's a function called 'operator new()' which is invoked by the
former each time you allocate memory using 'new'. The latter is also
called 'placement new', because you can give it an address where in
memory to allocate space. Note that it only allocates raw memory like
malloc and does not invoke any ctor calls.

Using malloc() in C++ is problematic for several reasons.
First, it doesn't invoke the constructor of the class you're
instantiating. This is obvious, since it's a C function, and C doesn't
know a thing about constructors.
In other words, it allocates raw memory (like placement new) and returns
a pointer of type void* to the newly allocated space.
Second, you therefore have to cast void* to the pointer type you need,
which you wouldn't have to do when using new instead (this is a GOOD thing).
Third, you have to pass malloc() the amount of memory you want to
allocate. This is error prone, if you don't give it the right size, you
will screw up. new figures out itself how much memory it will have to
allocate.

So, if you don't really care about the whole memory allocation stuff and
don't plan to implement your own allocators, or if you're even remotely
unsure when to use what, the rule of thumb is to prefer 'new' over
malloc() whenever possible.

--
Regards,
Matthias
Jul 23 '05 #16

P: n/a
Victor Bazarov wrote:
"Ron Natalie" <ro*@sensor.com> wrote...
Victor Bazarov wrote:
Dave Vandervies wrote:
[...] I never use malloc when I'm actually
writing C++ code, whether I'm doing OOP or not...
Really? Why? What's so bad about 'malloc'? It is
often _the_ function to call if you overload the
operator new...


Malloc doesn't create objects. It allocated bytes.
new creates objects.

No!... Really? I learn something new every day. So,
when I overload operator new, I need to call another
operator new, is that what you're saying?

No, and you know it. The new operator creates objects.
operator new is the unfortunately name C++ gives to the
memory allocator.
Jul 23 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.