Connecting Tech Pros Worldwide Forums | Help | Site Map

Malloc for object of an class

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

Mike Wahler
Guest
 
Posts: n/a
#2: Sep 2 '08

re: Malloc for object of an class



"Kislay" <kislaychandra@gmail.comwrote in message
news:dc06479e-8a58-450d-ba08-585fce4dd4c6@k36g2000pri.googlegroups.com...
Quote:
Can malloc be used to create/allocate memory to an object of a class .
Yes, but it's often insufficient.
Quote:
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


asm23
Guest
 
Posts: n/a
#3: Sep 2 '08

re: Malloc for object of an class


Mike Wahler wrote:
Quote:
"Kislay" <kislaychandra@gmail.comwrote in message
news:dc06479e-8a58-450d-ba08-585fce4dd4c6@k36g2000pri.googlegroups.com...
Quote:
>Can malloc be used to create/allocate memory to an object of a class .
>
Yes, but it's often insufficient.
>
Quote:
>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)
anon
Guest
 
Posts: n/a
#4: Sep 2 '08

re: Malloc for object of an class


asm23 wrote:
Quote:
Mike Wahler wrote:
Quote:
>"Kislay" <kislaychandra@gmail.comwrote in message
>news:dc06479e-8a58-450d-ba08-585fce4dd4c6@k36g2000pri.googlegroups.com...
Quote:
>>Can malloc be used to create/allocate memory to an object of a class .
>>
>Yes, but it's often insufficient.
>>
Quote:
>>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
zaimoni@zaimoni.com
Guest
 
Posts: n/a
#5: Sep 2 '08

re: Malloc for object of an class


On Sep 2, 9:41 am, Kislay <kislaychan...@gmail.comwrote:
Quote:
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.
Pete Becker
Guest
 
Posts: n/a
#6: Sep 2 '08

re: Malloc for object of an class


On 2008-09-02 13:35:56 -0400, zaimoni@zaimoni.com said:
Quote:
On Sep 2, 9:41 am, Kislay <kislaychan...@gmail.comwrote:
Quote:
>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)

zaimoni@zaimoni.com
Guest
 
Posts: n/a
#7: Sep 2 '08

re: Malloc for object of an class


On Sep 2, 12:50 pm, Pete Becker <p...@versatilecoding.comwrote:
Quote:
On 2008-09-02 13:35:56 -0400, zaim...@zaimoni.com said:
Quote:
Quote:
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?
Ian Collins
Guest
 
Posts: n/a
#8: Sep 2 '08

re: Malloc for object of an class


zaimoni@zaimoni.com wrote:
Quote:
>
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.
Kislay
Guest
 
Posts: n/a
#9: Sep 3 '08

re: Malloc for object of an class


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 .
Closed Thread