Connecting Tech Pros Worldwide Forums | Help | Site Map

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

RS
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi,

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

RS



Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

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


RS wrote:[color=blue]
> What is the difference between new() and malloc()?[/color]

'new' is the C++ way to create object in free store.
'mallow' is the C way to allocate some memory.
MatrixV
Guest
 
Posts: n/a
#3: Jul 23 '05

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


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...[color=blue]
> Hi,
>
> What is the difference between new() and malloc()?
>
> RS
>
>[/color]


E. Robert Tisdale
Guest
 
Posts: n/a
#4: Jul 23 '05

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


Victor Bazarov wrote:
[color=blue]
> RS wrote:
>[color=green]
>> What is the difference between new() and malloc()?[/color]
>
> 'new' is the C++ way to create object in free store.
> 'mallow' is the C way to allocate some memory.[/color]

You need to add malloc to your spell checker dictionary.
Victor Bazarov
Guest
 
Posts: n/a
#5: Jul 23 '05

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


E. Robert Tisdale wrote:[color=blue]
> Victor Bazarov wrote:
>[color=green]
>> RS wrote:
>>[color=darkred]
>>> What is the difference between new() and malloc()?[/color]
>>
>>
>> 'new' is the C++ way to create object in free store.
>> 'mallow' is the C way to allocate some memory.[/color]
>
>
> You need to add malloc to your spell checker dictionary.[/color]

No, I need to work shorter days. So do you, apparently.
Dave Vandervies
Guest
 
Posts: n/a
#6: Jul 23 '05

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


In article <3729k5F58niqnU1@individual.net>,
MatrixV <training@kcollege.com> wrote:[color=blue]
>That's explained by lots of books. You can even google it.
>Simply put, new is OOP, while malloc is not.[/color]

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 dj3vande@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
Victor Bazarov
Guest
 
Posts: n/a
#7: Jul 23 '05

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


Dave Vandervies wrote:[color=blue]
> [...] I never use malloc when I'm actually
> writing C++ code, whether I'm doing OOP or not...[/color]

Really? Why? What's so bad about 'malloc'? It is
often _the_ function to call if you overload the
operator new...
Dave Vandervies
Guest
 
Posts: n/a
#8: Jul 23 '05

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


In article <EOSOd.42803$NC6.29335@newsread1.mlpsca01.us.to.ve rio.net>,
Victor Bazarov <v.Abazarov@comAcast.net> wrote:[color=blue]
>Dave Vandervies wrote:[color=green]
>> [...] I never use malloc when I'm actually
>> writing C++ code, whether I'm doing OOP or not...[/color]
>
>Really? Why? What's so bad about 'malloc'? It is
>often _the_ function to call if you overload the
>operator new...[/color]

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 dj3vande@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
Basavaraj K
Guest
 
Posts: n/a
#9: Jul 23 '05

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


"MatrixV" <training@kcollege.com> wrote in message news:<3729k5F58niqnU1@individual.net>...[color=blue]
> 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...[color=green]
> > Hi,
> >
> > What is the difference between new() and malloc()?
> >
> > RS
> >
> >[/color][/color]
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
MatrixV
Guest
 
Posts: n/a
#10: Jul 23 '05

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


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" <dj3vande@csclub.uwaterloo.ca> ????
news:cugsnp$o77$1@rumours.uwaterloo.ca...[color=blue]
> In article <3729k5F58niqnU1@individual.net>,
> MatrixV <training@kcollege.com> wrote:[color=green]
> >That's explained by lots of books. You can even google it.
> >Simply put, new is OOP, while malloc is not.[/color]
>
> 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[/color]
dj3vande@csclub.uwaterloo.ca[color=blue]
> My personal best estimate is that 90% of existing C code is crap (F[/color]
various VO[color=blue]
> crap), and 90% of existing NotC code is crap too. (I expect this to be[/color]
true of[color=blue]
> non-existing code as well.) --Dimitri Mazuik in the scary devil[/color]
monastery


Default User
Guest
 
Posts: n/a
#11: Jul 23 '05

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



MatrixV wrote:[color=blue]
> You though you are doing quite well, but you are not. As Mr. Kirunge[/color]
said,[color=blue]
> malloc can't do many things, it just alloc a memory space. Of course[/color]
you can[color=blue]
> 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[/color]
this and[color=blue]
> it's hard to put them all here.
>
> "Dave Vandervies" <dj3vande@csclub.uwaterloo.ca> ????
> news:cugsnp$o77$1@rumours.uwaterloo.ca...[/color]


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



Brian

Ron Natalie
Guest
 
Posts: n/a
#12: Jul 23 '05

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


Victor Bazarov wrote:[color=blue]
> RS wrote:
>[color=green]
>> What is the difference between new() and malloc()?[/color]
>
>
> 'new' is the C++ way to create object in free store.
> 'mallow' is the C way to allocate some memory.[/color]

Mallow is a plant of the genus malva.
:-)
Ron Natalie
Guest
 
Posts: n/a
#13: Jul 23 '05

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


Victor Bazarov wrote:[color=blue]
> Dave Vandervies wrote:
>[color=green]
>> [...] I never use malloc when I'm actually
>> writing C++ code, whether I'm doing OOP or not...[/color]
>
>
> Really? Why? What's so bad about 'malloc'? It is
> often _the_ function to call if you overload the
> operator new...[/color]

Malloc doesn't create objects. It allocated bytes.
new creates objects.
Ron Natalie
Guest
 
Posts: n/a
#14: Jul 23 '05

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


Basavaraj K wrote:
[color=blue]
> -- calling constructor after allocating memory(new calls
> constructor as well)[/color]

The C++ memory allocation function (operator new) can be used to obtain
memory without constuction of an object.
[color=blue]
> -- Type safety: (new returns a pointer of the right type)[/color]

Which with the exception for the dastardly stupid case of POD types, is
initialize.
[color=blue]
> -- Overridability: (new is an operator that can be overridden)
>[/color]
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.
Victor Bazarov
Guest
 
Posts: n/a
#15: Jul 23 '05

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


"Ron Natalie" <ron@sensor.com> wrote...[color=blue]
> Victor Bazarov wrote:[color=green]
>> Dave Vandervies wrote:
>>[color=darkred]
>>> [...] I never use malloc when I'm actually
>>> writing C++ code, whether I'm doing OOP or not...[/color]
>>
>>
>> Really? Why? What's so bad about 'malloc'? It is
>> often _the_ function to call if you overload the
>> operator new...[/color]
>
> Malloc doesn't create objects. It allocated bytes.
> new creates objects.[/color]

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?


Matthias
Guest
 
Posts: n/a
#16: Jul 23 '05

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


RS wrote:[color=blue]
> Hi,
>
> What is the difference between new() and malloc()?
>
> RS
>
>[/color]

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
Ron Natalie
Guest
 
Posts: n/a
#17: Jul 23 '05

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


Victor Bazarov wrote:[color=blue]
> "Ron Natalie" <ron@sensor.com> wrote...
>[color=green]
>>Victor Bazarov wrote:
>>[color=darkred]
>>>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...[/color]
>>
>>Malloc doesn't create objects. It allocated bytes.
>>new creates objects.[/color]
>
>
> 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?
>
>[/color]
No, and you know it. The new operator creates objects.
operator new is the unfortunately name C++ gives to the
memory allocator.
Closed Thread


Similar C / C++ bytes