Suppose you have a memory pointer to some allocated memory and a class.
How can you call the constructor of the class so that it operates on your
memory?
The same question goes for the destructor.

Something like this:
class A;
void* tmp_buff;
((A*)tmp_buff)->A();
((A*)tmp_buff)->~A();

Re: Explicitly calling constructors/destructors by Doug

Doug
Thu Feb 24 16:30:20 CST 2005

Gabriel Bogdan wrote:

>Suppose you have a memory pointer to some allocated memory and a class.
>How can you call the constructor of the class so that it operates on your
>memory?
>The same question goes for the destructor.
>
>Something like this:
>class A;
>void* tmp_buff;
>((A*)tmp_buff)->A();

VC allows a syntax like that, but it isn't legal C++. You need to look up
"placement new" and pay attention to alignment and other considerations.

>((A*)tmp_buff)->~A();

That's legal, assuming tmp_buff contains a live A.

--
Doug Harrison
Microsoft MVP - Visual C++

Re: Explicitly calling constructors/destructors by Craig

Craig
Thu Feb 24 16:47:58 CST 2005

"Gabriel Bogdan" wrote:

> Suppose you have a memory pointer to some allocated memory and a
> class. How can you call the constructor of the class so that it
> operates on your memory?
>
> The same question goes for the destructor.
>
> Something like this:
> class A;
> void* tmp_buff;
> ((A*)tmp_buff)->A();
> ((A*)tmp_buff)->~A();


A few suggestions:

- Google for "placement new"

- See http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14

- You might want to look at using std::allocator<> in the STL

- You might want to check out Effective C++ by Meyers


The super-simple version of using placement new is...

class A;
void* tmp_buff = get_mem_from_somewhere();
A* a = new(tmp_buff) a;

...use a...

a->~A();
put_mem_back(tmp_buff);

Craig



Re: Explicitly calling constructors/destructors by Gabriel

Gabriel
Thu Feb 24 16:55:09 CST 2005


"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:h5ls11d6lc2iks3jgblat9118ocv4htbhs@4ax.com...
> Gabriel Bogdan wrote:
>
> >Suppose you have a memory pointer to some allocated memory and a class.
> >How can you call the constructor of the class so that it operates on your
> >memory?
> >The same question goes for the destructor.
> >
> >Something like this:
> >class A;
> >void* tmp_buff;
> >((A*)tmp_buff)->A();
>
> VC allows a syntax like that, but it isn't legal C++. You need to look up
> "placement new" and pay attention to alignment and other considerations.
>
> >((A*)tmp_buff)->~A();
>
> That's legal, assuming tmp_buff contains a live A.

Thanks for the replay,

I was curios because I did this a few yeas ago, but I no longer remember how
:(
Right now I am using a form of the new operator for the same thing.

It seems that you can use:
((A*)tmp_buff)->__ctor();

and as you pointed out:
((A*)tmp_buff)->~A();






Re: Explicitly calling constructors/destructors by Doug

Doug
Thu Feb 24 17:05:06 CST 2005

Gabriel Bogdan wrote:

>I was curios because I did this a few yeas ago, but I no longer remember how
>:(
>Right now I am using a form of the new operator for the same thing.

You need "placement new".

>It seems that you can use:
>((A*)tmp_buff)->__ctor();

That's totally non-standard, and there's no reason to use it.

--
Doug Harrison
Microsoft MVP - Visual C++

Re: Explicitly calling constructors/destructors by David

David
Fri Feb 25 12:08:42 CST 2005


"Gabriel Bogdan" <na@na.na> wrote in message
news:ep53r8rGFHA.2976@TK2MSFTNGP09.phx.gbl...

> Suppose you have a memory pointer to some allocated memory and a
> class.
> How can you call the constructor of the class so that it operates
> on your
> memory?

If you really are the sort of evil-doer who wants to perpetrate
such things, have a look at the MFC source code for CArray class.
IIRC it does all sorts of nasty things like this :-)

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm