C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to call a constructor explicitly if we want to allocate

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
shsingh
Guest





PostPosted: Wed Oct 18, 2006 7:57 am    Post subject: How to call a constructor explicitly if we want to allocate Reply with quote



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 initialized
properly as constructor same is not get called ( as per the behavior).

How to call a constructor explicitly if we want to allocate memory
using malloc ?
Back to top
Larry Smith
Guest





PostPosted: Wed Oct 18, 2006 8:11 am    Post subject: Re: How to call a constructor explicitly if we want to alloc Reply with quote



shsingh wrote:
Quote:
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 initialized
properly as constructor same is not get called ( as per the behavior).

How to call a constructor explicitly if we want to allocate memory
using malloc ?


Your create object on the heap with new, not malloc.

A *pA = new A;

You free the memory and object with delete.

delete pA;
Back to top
Victor Bazarov
Guest





PostPosted: Wed Oct 18, 2006 8:46 am    Post subject: Re: How to call a constructor explicitly if we want to alloc Reply with quote



shsingh wrote:
Quote:
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
initialized properly as constructor same is not get called ( as per
the behavior).

How to call a constructor explicitly if we want to allocate memory
using malloc ?

Use "placement new" (look it up).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Back to top
Bart
Guest





PostPosted: Wed Oct 18, 2006 9:10 am    Post subject: Re: How to call a constructor explicitly if we want to alloc Reply with quote

shsingh wrote:
Quote:
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 initialized
properly as constructor same is not get called ( as per the behavior).

How to call a constructor explicitly if we want to allocate memory
using malloc ?

You can use placement new.

buffer = malloc(/*...*/);
object = new (buffer) MyClass(/*...*/);

But later you have to call the destructor explicitly too:

object->~MyClass();
free(buffer);

Regards,
Bart.
Back to top
Alf P. Steinbach
Guest





PostPosted: Sat Oct 21, 2006 9:10 am    Post subject: Re: How to call a constructor explicitly if we want to alloc Reply with quote

* Kai-Uwe Bux:
[about "can't call constructor" urban myth]

I recently ran into yet another formulation in the standard that uses
the "call" terminology, namely §1.9/15, about an initializer like

Foo object( arg1, arg2, arg3 ... );

"the resulting construct is a function call upon a constructor function,
with expression-list as an argument list; such a function call is a
full-expression".

Can't be very much more clear than that, I think. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Guest






PostPosted: Sat Oct 21, 2006 9:10 am    Post subject: Re: How to call a constructor explicitly if we want to alloc Reply with quote

Quote:
How to call a constructor explicitly if we want to allocate
memory using malloc ?

This thread has been an excellent read for me. "placement new" is a
concept I had not heard of before. Since I do some kernel level
development it is an excellent language feature because you often are
given context buffer space that you overlay a structure onto. Without
placement new, there ar two suboptimal solutions: either avoid
constructors or put a pointer to the object (instead of the object
itself) in the context buffer and new/delete it.

If there are any other concepts that might especially be useful in
kernel level development, please mention.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.