Mail Archives: djgpp/2001/01/20/20:36:29
Thanks
P.S.
I want to this for specific purpose :
I have big array (about 1MB) with my debug informations. At program start up
I create one big chunk of memory, then lock it. All debug informations must
be store in this area. So I want to use placement version of new( ). I have
few variable working like litle memory manage for this area so I know exacly
where I want my debug class to be placed.
Użytkownik Steamer <dontmailme AT iname DOT com> w wiadomo¶ci do grup dyskusyjnych
napisał:3a670fdb DOT 10141607 AT news DOT freeserve DOT net...
> Rafal Maj wrote:
>
> > how to create object of my class "Class" using new, so this object will
be
> > placed in some allocated before memory area ?
> > For example :
> >
> > char *chunk;
> > chunk = new char[1024]; // sizeof(Class)<1024
> > --> ptr = new Class(1,2); <---
> > // I want object to be placed in chunk, so now ptr == chunk
> > ptr->fun();
> > delete ptr;
>
> If you really want to do something like this, then you need
> 'placement' new, which is designed for just this purpose (placing
> an object at a given location). E.g.
>
> void *chunk = malloc(1024);
> if (chunk)
> Class *ptr = new(chunk) Class(1,2);
>
> Note that I've used malloc(), because new char[1024] might not
> necessarily return a pointer that is correctly aligned for Class
> (although I think it will in DJGPP).
>
> Now you can do
>
> ptr->fun();
>
> or whatever. But don't try to delete ptr - instead you have to
> call the Class destructor directly:
>
> ptr->~Class();
>
> Take a look at section 11.10 of the C++ FAQ Lite, which discusses
> placement new (http://www.parashift.com/c++-faq-lite/).
- Raw text -