From: "Rafal Maj" Newsgroups: comp.os.msdos.djgpp Subject: Odp: Using new Date: Sat, 20 Jan 2001 21:21:09 +0100 Organization: Academic Computer Center CYFRONET AGH Lines: 50 Message-ID: <94crrs$ot1$1@info.cyf-kr.edu.pl> References: <944rio$k8r$1 AT info DOT cyf-kr DOT edu DOT pl> <3a670fdb DOT 10141607 AT news DOT freeserve DOT net> NNTP-Posting-Host: d-94-53-13.cyfronet.krakow.pl Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-2" Content-Transfer-Encoding: 8bit X-Trace: info.cyf-kr.edu.pl 980021948 25505 149.156.1.173 (20 Jan 2001 20:19:08 GMT) X-Complaints-To: news AT cyf-kr DOT edu DOT pl NNTP-Posting-Date: 20 Jan 2001 20:19:08 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com 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 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/).