473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

RS
Hi,

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

RS
Jul 23 '05 #1
16 6746
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
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
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
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
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
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
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
"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
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

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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
15
by: Mohanasundaram | last post by:
Hi All, What is the difference between malloc and calloc other than the point that calloc will initialize the memory to all zeros? This was an interview question for me. All the books and...
4
by: Chris | last post by:
This is the code: ###################### #include <stdlib.h> using namespace std; class intlist { unsigned int length; int * list; public: intlist() {
67
by: neilcancer | last post by:
i come from china,and i'm sorry that my english is very poor. now i'm studing data structure and i met some problem about c language. could you tell me what will happen after i use free()? i...
13
by: manish sahu | last post by:
difference between calloc() and malloc()
15
by: Pranav | last post by:
class just{ public : int x; just(){ x=1234; } just(int i){ x = i;} ~just(){ cout << " Here We Are\n" ;} }; int main() {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.