Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 25th, 2006, 03:35 PM
codefuns
Guest
 
Posts: n/a
Default 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?

  #2  
Old January 25th, 2006, 03:35 PM
Markus Moll
Guest
 
Posts: n/a
Default 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;
  #3  
Old January 25th, 2006, 03:45 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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
  #4  
Old January 25th, 2006, 03:55 PM
Markus Moll
Guest
 
Posts: n/a
Default 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

  #5  
Old January 25th, 2006, 03:55 PM
mlimber
Guest
 
Posts: n/a
Default 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]

See these FAQs:

http://www.parashift.com/c++-faq-lit...html#faq-11.10
http://www.parashift.com/c++-faq-lit...html#faq-11.14

Cheers! --M

  #6  
Old January 25th, 2006, 04:05 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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
  #7  
Old January 25th, 2006, 04:05 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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!
  #8  
Old January 25th, 2006, 04:55 PM
codefuns
Guest
 
Posts: n/a
Default 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.

  #9  
Old January 25th, 2006, 05:05 PM
Markus Moll
Guest
 
Posts: n/a
Default 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

  #10  
Old January 25th, 2006, 08:55 PM
Bo Persson
Guest
 
Posts: n/a
Default 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


  #11  
Old January 26th, 2006, 03:25 AM
Daniel T.
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles