473,769 Members | 6,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

whats the advantageof malloc over new in c++.

whats the beauty of "malloc" over "new" why its helpful for
programmer.for its own memory area.??
Aug 19 '08 #1
26 3845
On 19 Aug, 09:52, Muzammil <muzammilPeer.. .@gmail.comwrot e:
whats the beauty of "malloc" over "new" why its helpful for
programmer.for *its own memory area.??
malloc() doesn't get used in C++ programs very much.
malloc() just allocates memory. new allocates memory
and constructs the object (by calling the constructor).
Placement new runs the constructor on the memory provided.

malloc() is mainly provided for historical compatibility
with C. new may use malloc() internally (it doesn't have to).

the main use for malloc(), I can see, is if memory is to
be passed to a C function and to be freed by the C function.
--
Nick Keighley
Aug 19 '08 #2
On Aug 19, 5:57 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:
On 19 Aug, 09:52, Muzammil <muzammilPeer.. .@gmail.comwrot e:
whats the beauty of "malloc" over "new" why its helpful for
programmer.for its own memory area.??

malloc() doesn't get used in C++ programs very much.
malloc() just allocates memory. new allocates memory
and constructs the object (by calling the constructor).
Placement new runs the constructor on the memory provided.

malloc() is mainly provided for historical compatibility
with C. new may use malloc() internally (it doesn't have to).

the main use for malloc(), I can see, is if memory is to
be passed to a C function and to be freed by the C function.
+ malloc provides memory that _may_ be able to be grown using
realloc() without having to copy the content

+ new can be overridden to do all manner of things - which is normally
a good thing - but malloc() might occasionally be useful as a way to
bypass this (though you can't generally be sure that the "normal"
malloc function will receive your call - there are lots of ways of
installing alternatives)

+ you can free malloced memory without having the destructor called
(not normally a good idea, but may allow optimisations in certain
usages)

Still, the short story is don't use malloc/realloc and free unless you
find a reason to have to.

Tony
Aug 19 '08 #3
On 2008-08-19 11:57, to***********@y ahoo.co.uk wrote:
On Aug 19, 5:57 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:
>On 19 Aug, 09:52, Muzammil <muzammilPeer.. .@gmail.comwrot e:
whats the beauty of "malloc" over "new" why its helpful for
programmer.for its own memory area.??

malloc() doesn't get used in C++ programs very much.
malloc() just allocates memory. new allocates memory
and constructs the object (by calling the constructor).
Placement new runs the constructor on the memory provided.

malloc() is mainly provided for historical compatibility
with C. new may use malloc() internally (it doesn't have to).

the main use for malloc(), I can see, is if memory is to
be passed to a C function and to be freed by the C function.

+ malloc provides memory that _may_ be able to be grown using
realloc() without having to copy the content
Provided that the content of that memory does not have to be constructed.

--
Erik Wikström
Aug 19 '08 #4
Erik Wikström wrote:
>+ malloc provides memory that _may_ be able to be grown using
realloc() without having to copy the content

Provided that the content of that memory does not have to be constructed.
You could always use placement new to construct that memory. (OTOH if
you must construct it, it starts making little sense to not to just use
'new' rather than 'malloc'.)
Aug 19 '08 #5
"Muzammil" <mu************ *@gmail.comwrot e in message news:b1******** *************** ***********@56g 2000hsm.googleg roups.com...
whats the beauty of "malloc" over "new" why its helpful for
programmer.for its own memory area.??
memory allocated by malloc can be reallocated with realloc.
malloc doesnt throw an exception if the allocation fails.
Most runtimes implement a rich set of heap interrogation / debugging methods that can be usefull, which work principally on memory allocated with malloc.

Aug 19 '08 #6
On 19 Aug., 11:59, "Chris Becke" <chris.be...@gm ail.comwrote:
"Muzammil" <muzammilPeer.. .@gmail.comwrot e in messagenews:b1* *************** *************** ***@56g2000hsm. googlegroups.co m...
whats the beauty of "malloc" over "new" why its helpful for
programmer.for *its own memory area.??

memory allocated by malloc can be reallocated with realloc.
But this is not really useful in a C++ context.
malloc doesnt throw an exception if the allocation fails.
Neither does new if you use the nothrow version (although I've
difficulties finding places, where I'd care about that).
Most runtimes implement a rich set of heap interrogation / debugging methods that can be
usefull, which work principally on memory allocated with malloc.
You can always base new on malloc if you want to - this allows you to
use the exact same methods, should they not already be available for
new/delete.
/Peter

Aug 19 '08 #7
On 19 Aug., 10:52, Muzammil <muzammilPeer.. .@gmail.comwrot e:
whats the beauty of "malloc" over "new" why its helpful for
programmer.for *its own memory area.??
The only reason to use malloc is for interfacing with C.

/Peter
Aug 19 '08 #8
Chris Becke wrote:
"Muzammil" <mu************ *@gmail.comwrot e in message
news:b1******** *************** ***********@56g 2000hsm.googleg roups.com...
>whats the beauty of "malloc" over "new" why its helpful for
programmer.f or its own memory area.??

memory allocated by malloc can be reallocated with realloc.
Which just sometimes might realloc in place, otherwise it has to copy
everything. And this doesn't work for anything with a constructor.
malloc doesnt throw an exception if the allocation fails.
A clear disadvantage. :-)

You have to check the result manually instead.
Most runtimes implement a rich set of heap interrogation /
debugging methods that can be usefull, which work principally on
memory allocated with malloc.
If you use std::vector you probably will never have to debug the
memory allocation at all.
Bo Persson


Aug 19 '08 #9
On Aug 20, 6:02 am, "Bo Persson" <b...@gmb.dkwro te:
Chris Becke wrote:
"Muzammil" <muzammilPeer.. .@gmail.comwrot e in message
news:b1******** *************** ***********@56g 2000hsm.googleg roups.com...
whats the beauty of "malloc" over "new" why its helpful for
programmer.for its own memory area.??
memory allocated by malloc can be reallocated with realloc.

Which just sometimes might realloc in place, otherwise it has to copy
everything. And this doesn't work for anything with a constructor.
"just sometimes might" is trivialising the benefit a bit ;-). Anyway,
only the additional elements need then be manually constructed with
placement new... and this doesn't necessarily need to be done using a
default constructor before the meaningful values are available which
can be an advantage... so at least this two-step process can work for
some things with constructors.

Perhaps a bigger issue is that moved existing elements aren't copy
constructed so references/pointers stored therein may end up invalid.

Tony
Aug 20 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
683
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But has it allocated memory or what?
231
23247
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
7
2216
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
5
2834
by: kernel.lover | last post by:
hello, I want to know if a fuction say malloc is declared as void *malloc() then whats the significance of void here. Does void * is used when function has the flexibility to return any type of value by casting that functions with appropriate data type?
4
2282
by: lothar.behrens | last post by:
Hi, my own stream implementation writes correctly, but it does not read all back. Why ? Thanks, Lothar Here is the output: 'Testdata1: ', 0
15
2589
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that malloc always returned null at this moment and the program exited (even though if I malloc'ed only 20 bytes or something)... Then I googled for this problem and found something about a memory pool??? Is that standard C? I didn't understand it,...
8
2237
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
68
15712
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
71
19129
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a problem? I cannot see why using malloc instead of new does not give the same result.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.