Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to call a constructor explicitly if we want to allocate memory using malloc ?

Reply
Thread Tools

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

 
 
shsingh
Guest
Posts: n/a
 
      10-18-2006
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 ?

 
Reply With Quote
 
 
 
 
Larry Smith
Guest
Posts: n/a
 
      10-18-2006
shsingh wrote:
> 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;
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-18-2006
shsingh wrote:
> 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


 
Reply With Quote
 
Bart
Guest
Posts: n/a
 
      10-18-2006

shsingh wrote:
> 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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-18-2006
shsingh wrote:
> 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't call a constructor. You can construct an object in
memory you provide (from malloc or anyplace else) with
placement new:

#include <new>

void* memory = malloc(sizeof T);
T* tp = new (memory) T;

 
Reply With Quote
 
zouyongbin
Guest
Posts: n/a
 
      10-18-2006

Bart wrote:
> shsingh wrote:
> > 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.


Is that correct to construct the object on the memory allocated by
malloc? Is that behavior defined in standard C++? I think that the
memory should be allocated by operator new if you want to use placement
new.

Best Regards,
Robin.

 
Reply With Quote
 
Bart
Guest
Posts: n/a
 
      10-18-2006
zouyongbin wrote:
> Bart wrote:
> > shsingh wrote:
> > > 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.

>
> Is that correct to construct the object on the memory allocated by
> malloc? Is that behavior defined in standard C++? I think that the
> memory should be allocated by operator new if you want to use placement
> new.


No. Placement new was invented specifically so you can use your own
memory allocator.

The memory doesn't even have to be "allocated" at all. If you were to
write an OS kernel in C++ you could just construct some object at
whatever physical address you like without ever allocating any memory.

Regards,
Bart.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-18-2006
Ron Natalie wrote:

> shsingh wrote:
>> 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't call a constructor.


Can you call function? or is all that *you* can do writing an expression in
your code and then, if required, the implementation will call a function in
the course of the evaluation of that expression? As far as I know, the
standard does not set linguistic precedence for the using the active voice
(we call a function/constructor) as opposed to the passive voice (a
function/constructor is called) in either case. So do you take a
linguistice license in the case of functions but not in the case of
constructors? If so, why?


> You can construct an object in
> memory you provide (from malloc or anyplace else) with
> placement new:
>
> #include <new>
>
> void* memory = malloc(sizeof T);
> T* tp = new (memory) T;



Best

Kai-Uwe Bux

 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      10-18-2006
Kai-Uwe Bux wrote:
> Ron Natalie wrote:
>
>> shsingh wrote:
>>> 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't call a constructor.

>
> Can you call function? or is all that *you* can do writing an
> expression in your code and then, if required, the implementation
> will call a function in the course of the evaluation of that
> expression? As far as I know, the standard does not set linguistic
> precedence for the using the active voice (we call a
> function/constructor) as opposed to the passive voice (a
> function/constructor is called) in either case. So do you take a
> linguistice license in the case of functions but not in the case of
> constructors? If so, why?
>


You can call a function.

You cannot call a constructor directly, because it doesn't have a
name. Most attempts to "call a constructor" will create a temporary of
the class type instead.

class C {};

C::C(); // creates a temporary of class C


In practice, you invoke the constructor as part of executing a new
expression, like placement new.



Bo Persson


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-18-2006
Bo Persson wrote:
> Kai-Uwe Bux wrote:
>> Ron Natalie wrote:
>>
>>> shsingh wrote:
>>>> 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't call a constructor.

>>
>> Can you call function? or is all that *you* can do writing an
>> expression in your code and then, if required, the implementation
>> will call a function in the course of the evaluation of that
>> expression? As far as I know, the standard does not set linguistic
>> precedence for the using the active voice (we call a
>> function/constructor) as opposed to the passive voice (a
>> function/constructor is called) in either case. So do you take a
>> linguistice license in the case of functions but not in the case of
>> constructors? If so, why?
>>

>
> You can call a function.
>
> You cannot call a constructor directly, because it doesn't have a
> name. Most attempts to "call a constructor" will create a temporary of
> the class type instead.
>
> class C {};
>
> C::C(); // creates a temporary of class C



Come on, can't you spot a troll?

If you create a temporary, doesn't it in fact mean you managed to call
a constructor? Kai-Uwe is just f****ng with you. Now, you've joined
the debacle by introducing your "directly" into the argument. So what
does "you cannot call a constructor directly" actually mean? Does it
mean you *can* call it, or that you *cannot* call it? Drop the
"directly" and explain. Can you? You can't. Directly, I mean.
(now that's what I call a word play)

> In practice, you invoke the constructor as part of executing a new
> expression, like placement new.


"Invoke", "call"... Isn't it all the same thing? At some point in
the past I grew tired of this debate and now try not to beat this
half-dead horse. Refraining from torturing the poor animal is highly
recommended to all readers.

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to call templated constructor explicitly? Lawrence Spector C++ 3 07-16-2007 07:16 PM
How to allocate mem without using malloc() & free without using free() Rajshekhar C Programming 5 03-29-2005 06:03 PM
Does malloc() allocate memory only in powers of 2...? dsptechie C Programming 3 08-10-2004 05:22 AM
Does malloc() allocate memory only in powers of 2...? dsptechie C Programming 2 08-09-2004 10:54 AM



Advertisments