Question about operator new 
January 25th, 2006, 02:35 PM
| | | Question about operator new
when I use the placement form new to new a object(actually it does not
alloc memory space for the object), how should I delete this object
accordingly? | 
January 25th, 2006, 02:35 PM
| | | Re: Question about operator new
Hi
codefuns wrote:
[color=blue]
> when I use the placement form new to new a object(actually it does not
> alloc memory space for the object), how should I delete this object
> accordingly?[/color]
By a pseudo-destructor call.
T object; | 
January 25th, 2006, 02:45 PM
| | | Re: Question about operator new
codefuns wrote:[color=blue]
> when I use the placement form new to new a object(actually it does not
> alloc memory space for the object), how should I delete this object
> accordingly?
>[/color]
No, you should only call its destructor explicitly.
MyClass *myObj = new (somestorage) MyClass();
...
myObj->~MyClass(); // destroy
The storage is unrelated to the lifetime of 'myObj' and how you obtain it
or dispose of it is immaterial here.
V | 
January 25th, 2006, 02:55 PM
| | | Re: Question about operator new
Hi
codefuns wrote:
[color=blue]
> when I use the placement form new to new a object(actually it does not
> alloc memory space for the object), how should I delete this object
> accordingly?[/color]
By a pseudo-destructor call.
(sorry, hit the wrong key... I'll continue)
T object;
object.~T();
new (&object) T();
But why do you want to do this in the first place? If you don't know how, I
suspect you shouldn't...
Markus | 
January 25th, 2006, 03:05 PM
| | | Re: Question about operator new
Markus Moll wrote:[color=blue]
> codefuns wrote:
>
>[color=green]
>>when I use the placement form new to new a object(actually it does not
>>alloc memory space for the object), how should I delete this object
>>accordingly?[/color]
>
>
> By a pseudo-destructor call.
>
> (sorry, hit the wrong key... I'll continue)
>
> T object;
> object.~T();
> new (&object) T();
>
> But why do you want to do this in the first place? If you don't know how, I
> suspect you shouldn't...[/color]
I think you mistakenly answered how to use placement 'new' to "renew", or
"reconstruct", an object that is already in existence. The OP, IMO, did
not ask that. 'codefuns' simply asked what to do with the pointer which
was obtained from a 'placement new', whether it should be 'delete'd.
V | 
January 25th, 2006, 03:05 PM
| | | Re: Question about operator new
mlimber wrote:[color=blue]
> codefuns wrote:[color=green]
>>[..]?[/color]
> See these FAQs:
> [..][/color]
I need to freshen up on what's in our FAQ so I don't answer those
questions again and again and again and again and again... Thanks, M! | 
January 25th, 2006, 03:55 PM
| | | Re: Question about operator new
I just need a template class that has both functions of stl classes
vector and map. the data is stored on a array and and be indexed by a
key. so that the performances of random access and key access are both
good.
I have try to delete the object by calling the destructor directly, it
seems work.
thanks peoples all here. | 
January 25th, 2006, 04:05 PM
| | | Re: Question about operator new
Hi
Victor Bazarov wrote:
[color=blue]
> I think you mistakenly answered how to use placement 'new' to "renew", or
> "reconstruct", an object that is already in existence. The OP, IMO, did
> not ask that. 'codefuns' simply asked what to do with the pointer which
> was obtained from a 'placement new', whether it should be 'delete'd.[/color]
I stand corrected.
I was misled by the phrase "when I use the placement form new to new a
object" and thought he was talking about "renewing". I should pay more
attention next time... :-)
Markus | 
January 25th, 2006, 07:55 PM
| | | Re: Question about operator new
"Victor Bazarov" <v.Abazarov@comAcast.net> skrev i meddelandet
news:D3NBf.277$a97.135@newsread1.mlpsca01.us.to.ve rio.net...[color=blue][color=green]
>>
>> T object;
>> object.~T();
>> new (&object) T();
>>
>> But why do you want to do this in the first place? If you don't
>> know how, I
>> suspect you shouldn't...[/color][/color]
:-)
[color=blue]
>
> I think you mistakenly answered how to use placement 'new' to
> "renew", or
> "reconstruct", an object that is already in existence. The OP, IMO,
> did
> not ask that. 'codefuns' simply asked what to do with the pointer
> which
> was obtained from a 'placement new', whether it should be 'delete'd.[/color]
And to the original poster, we might add that although the code above
works, a simpler way to reinitialize an object is to simply assign it
an empty value, like in
object = T();
Bo Persson | 
January 26th, 2006, 02:25 AM
| | | Re: Question about operator new
In article <1138207163.361300.190830@g49g2000cwa.googlegroups .com>,
"codefuns" <codefuns@gmail.com> wrote:
[color=blue]
> I just need a template class that has both functions of stl classes
> vector and map. the data is stored on a array and and be indexed by a
> key. so that the performances of random access and key access are both
> good.[/color]
I am curious now. So you have a container where each element has two
keys? How would the key access of your container be any better than it
is for a std::map, you still have to perform a binary search don't you?
What is it exactly that makes you think a std::map wouldn't work, have
you tried it?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|