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

V

Victor Bazarov

RS said:
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.
 
M

MatrixV

That's explained by lots of books. You can even google it.
Simply put, new is OOP, while malloc is not.
 
E

E. Robert Tisdale

Victor said:
'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.
 
D

Dave Vandervies

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
 
V

Victor Bazarov

Dave said:
[...] 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...
 
D

Dave Vandervies

Dave said:
[...] 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
 
B

Basavaraj K

MatrixV said:
That's explained by lots of books. You can even google it.
Simply put, new is OOP, while malloc is not.
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
 
M

MatrixV

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 said:
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 (e-mail address removed)
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
 
D

Default User

MatrixV said:
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" <[email protected]> ????
news:[email protected]...


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



Brian
 
R

Ron Natalie

Victor said:
Dave said:
[...] 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.
 
R

Ron Natalie

Basavaraj said:
-- 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.
 
V

Victor Bazarov

Ron Natalie said:
Victor said:
Dave said:
[...] 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?
 
M

Matthias

RS said:
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.
 
R

Ron Natalie

Victor said:
Ron Natalie said:
Victor said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top