473,785 Members | 2,811 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
26 3847
On 20 Aug., 02:07, tony_in_da...@y ahoo.co.uk wrote:
On Aug 20, 6:02 am, "Bo Persson" <b...@gmb.dkwro te:
Chris Becke wrote:
"Muzammil" <muzammilPeer.. .@gmail.comwrot e in message
>news:b1******* *************** ************@56 g2000hsm.google groups.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.

"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
If you are depending on the realloc-trick, most probably you are
implementing an array. In that case, std::vector is the place to go.
std::vector can take of advantage of whatever low-level memory
management the platform has and do an "in-place realloc" if such a
beast exists.

/Peter
Aug 20 '08 #11
On Aug 20, 9:53 am, peter koch <peter.koch.lar ...@gmail.comwr ote:
On 20 Aug., 02:07, tony_in_da...@y ahoo.co.uk wrote:
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

If you are depending on the realloc-trick, most probably you are
implementing an array. In that case, std::vector is the place to go.
std::vector can take of advantage of whatever low-level memory
management the platform has and do an "in-place realloc" if such a
beast exists.

/Peter
This should help.

http://www.parashift.com/c++-faq-lit....html#faq-16.4

Sumanth
Aug 20 '08 #12
peter koch wrote:
The only reason to use malloc is for interfacing with C.
Well, I can imagine a custom allocator using malloc rather than new
because the former allocates uninitialized memory, which is exactly what
a custom allocator should do (it's up to the calling code to actually
initialize the allocated memory).
Aug 20 '08 #13
to***********@y ahoo.co.uk wrote:
On Aug 20, 6:02 am, "Bo Persson" <b...@gmb.dkwro te:
>Chris Becke wrote:
>>"Muzammil" <muzammilPeer.. .@gmail.comwrot e in message
news:b1****** *************** *************@5 6g2000hsm.googl egroups.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.

"just sometimes might" is trivialising the benefit a bit ;-).
Yes.

The problem is that you don't know whether it works or not. If it is
important to realloc very fast, you have to make sure it works. If it
isn't important, it just isn't.
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.
But realloc() takes a void* pointer to the memory block, so it doesn't
know what kind of objects it contains.
>
Perhaps a bigger issue is that moved existing elements aren't copy
constructed so references/pointers stored therein may end up
invalid.
Rather important, yes. :-)
Bo Persson
Aug 20 '08 #14
On 20 Aug., 17:08, Juha Nieminen <nos...@thanks. invalidwrote:
peter koch wrote:
The only reason to use malloc is for interfacing with C.

* Well, I can imagine a custom allocator using malloc rather than new
because the former allocates uninitialized memory, which is exactly what
a custom allocator should do (it's up to the calling code to actually
initialize the allocated memory).
In this case operator new serves just as well.

/Peter
Aug 20 '08 #15
On Aug 21, 1:51*am, "Bo Persson" <b...@gmb.dkwro te:
tony_in_da...@y ahoo.co.uk wrote:
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.

But realloc() takes a void* pointer to the memory block, so it doesn't
know what kind of objects it contains.
Perhaps I didn't explain that well... sorry... off-the-top-of-my head
coding here:

if (X* p = (X*)realloc(old _p, sizeof(X) * new_capacity))
for (int n = old_size; n < new_capacity; ++n)
new(p[n]) X(fn(n)); // meaningful construction...

Cheers,
Tony
Aug 20 '08 #16
A clear disadvantage. :-)

You have to check the result manually instead.
predictable program flow is a disadvantage? Not even google likes c++ exceptions.
Aug 21 '08 #17
Chris Becke wrote:
>A clear disadvantage. :-)

You have to check the result manually instead.

predictable program flow is a disadvantage? Not even google likes c++ exceptions.
Their loss.

--
Ian Collins.
Aug 21 '08 #18
In article <bd************ *************** *******@x16g200 0prn.googlegrou ps.com>,
<to***********@ yahoo.co.ukwrot e:
>On Aug 21, 1:51*am, "Bo Persson" <b...@gmb.dkwro te:
>tony_in_da...@ yahoo.co.uk wrote:
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.

But realloc() takes a void* pointer to the memory block, so it doesn't
know what kind of objects it contains.

Perhaps I didn't explain that well... sorry... off-the-top-of-my head
coding here:

if (X* p = (X*)realloc(old _p, sizeof(X) * new_capacity))
for (int n = old_size; n < new_capacity; ++n)
new(p[n]) X(fn(n)); // meaningful construction...
Hmm, tell me what happens in the code above if realloc unfortunately
fail to be able to grow in place and need to malloc new memory at a
different place?

Guess if X is memcpy-able you'll survive but if not, you are dead.

What advantage does this has over (apart from being more bug prone):

std:vector<Xv;
// v.reserve(somen umber); //optional
// ...
for(size_t n = v.size(); v < nerw_capacity ; ++v)
v.append(X(f(n) ));
Yan
Aug 21 '08 #19
On 21 Aug., 09:26, "Chris Becke" <chris.be...@gm ail.comwrote:
A clear disadvantage. *:-)
You have to check the result manually instead.

predictable program flow is a disadvantage? Not even google likes c++ exceptions.
I don't know about google, but using an exception is the preferred and
standard way to cope with an error that can't be handled locally. If
you believe that you can handle the exception locally, nothing
prevents you from using the non-throwing version of new.

/Peter
Aug 21 '08 #20

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
2835
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
2591
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
15715
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
19135
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
9645
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
10147
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
10090
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
8971
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
7499
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
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.