473,385 Members | 1,506 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,385 software developers and data experts.

Malloc for object of an class

Can malloc be used to create/allocate memory to an object of a class .
We use new , but can we use malloc ?
Sep 2 '08 #1
8 8536

"Kislay" <ki***********@gmail.comwrote in message
news:dc**********************************@k36g2000 pri.googlegroups.com...
Can malloc be used to create/allocate memory to an object of a class .
Yes, but it's often insufficient.
We use new , but can we use malloc ?
'new' allocates memory and invokes your
object's constructor. 'malloc()' allocates
memory, and that's all.

-Mike
Sep 2 '08 #2
Mike Wahler wrote:
"Kislay" <ki***********@gmail.comwrote in message
news:dc**********************************@k36g2000 pri.googlegroups.com...
>Can malloc be used to create/allocate memory to an object of a class .

Yes, but it's often insufficient.
>We use new , but can we use malloc ?

'new' allocates memory and invokes your
object's constructor. 'malloc()' allocates
memory, and that's all.

-Mike

That's why C++ introduce "new" keyword. This is the standard method. But
if you can malloc and call the constructor manually, you can test it
yourself.(I bet this doesn't work well)
Sep 2 '08 #3
asm23 wrote:
Mike Wahler wrote:
>"Kislay" <ki***********@gmail.comwrote in message
news:dc**********************************@k36g200 0pri.googlegroups.com...
>>Can malloc be used to create/allocate memory to an object of a class .

Yes, but it's often insufficient.
>>We use new , but can we use malloc ?

'new' allocates memory and invokes your
object's constructor. 'malloc()' allocates
memory, and that's all.

-Mike

That's why C++ introduce "new" keyword. This is the standard method. But
if you can malloc and call the constructor manually, you can test it
yourself.(I bet this doesn't work well)
With placement new it would work, but you would have to do something
like explained here:
http://www.parashift.com/c++-faq-lit...html#faq-11.10
Sep 2 '08 #4
On Sep 2, 9:41 am, Kislay <kislaychan...@gmail.comwrote:
Can malloc be used to create/allocate memory to an object of a class .
We use new , but can we use malloc ?
Operator new is required for creating a proper class (not Plain Old
Data). But there are several variations on the new operator.

Read the page referenced in another thread to the C++ FAQ Lite. The
explanation there is thorough and well-written.

More directly:
If all you want is a C-like testing for the NULL pointer because
there's a good reason to avoid throwing std::bad_alloc, new(nothrow)
is a much more reasonable choice than malloc followed by placement
new.

MyClass* tmp = new MyClass; /* throws std::bad_alloc on failure */
MyClass* tmp2 = new(nothrow) MyClass; /* returns NULL on failure;
occasionally useful in normal code */

In my work, I start with new, and defer the decision to use
new(nothrow) until when optimization makes sense. At that time, I
look at how many sources of thrown std::bad_alloc are enveloped by the
try-catch block. If there's only one, there's a good chance of an
object-file size reduction using new(nothrow) rather than new.
Otherwise, the extra code complexity in handling NULL returns
generally isn't worth it.

Using malloc, followed by placement new, is a decidedly low-level
approach that is asking for bugs. About the only thing I'd use
placement new and placement delete for for is implementing STL-like
containers.
Sep 2 '08 #5
On 2008-09-02 13:35:56 -0400, za*****@zaimoni.com said:
On Sep 2, 9:41 am, Kislay <kislaychan...@gmail.comwrote:
>Can malloc be used to create/allocate memory to an object of a class .
We use new , but can we use malloc ?

Operator new is required for creating a proper class (not Plain Old
Data).
No, it's not required. Just most common. malloc and placement new is a
workable substitute.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 2 '08 #6
On Sep 2, 12:50 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-09-02 13:35:56 -0400, zaim...@zaimoni.com said:
Operator new is required for creating a proper class (not Plain Old
Data).

No, it's not required. Just most common. malloc and placement new is a
workable substitute.
Please straighten me out here. In what sense is placement new not an
operator?
Sep 2 '08 #7
za*****@zaimoni.com wrote:
>
Using malloc, followed by placement new, is a decidedly low-level
approach that is asking for bugs. About the only thing I'd use
placement new and placement delete for for is implementing STL-like
containers.
Placement new is also the correct solution for situation where memory
allocation is not required. Mapping an object over a memory mapped
hardware device or byte stream is one such use.

--
Ian Collins.
Sep 2 '08 #8
I tried using malloc instead of new, it works when the constructor is
NOT parametrized . In case of a parametrized constructor , the value
of the object is still null .
Sep 3 '08 #9

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

Similar topics

19
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...
22
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
7
by: Berk Birand | last post by:
Hi, For an assignement that I have, I have to write a function that is used for duplicating arrays of some objects (called Product). It is supposed to be something similar to strndup, but taking...
13
by: mike79 | last post by:
hi all.. if I wanted to malloc a struct, say the following: struct myStruct1 { int number; char *string; }
7
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...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
71
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...
3
by: somenath | last post by:
Hi All, I need to compile c code using c++ compiler .As i need to create object of classes inside the c functions I am using malloc and free. 1) My doubt is it ok to compile "c"code using...
11
by: Jason | last post by:
I've inherited some old C code that I've been tasked to convert to C++. I've been running into some problems with memory allocation. First of all, is there any problems using "malloc" in C++? I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.