Malloc for object of an class

K

Kislay

Can malloc be used to create/allocate memory to an object of a class .
We use new , but can we use malloc ?
 
A

asm23

Mike said:
Yes, but it's often insufficient.


'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)
 
Z

zaimoni

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.
 
Z

zaimoni

On 2008-09-02 13:35:56 -0400, (e-mail address removed) said:

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?
 
I

Ian Collins

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.
 
K

Kislay

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 .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top