Connecting Tech Pros Worldwide Help | Site Map

Question about operator new

codefuns
Guest
 
Posts: n/a
#1: Jan 25 '06
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?

Markus Moll
Guest
 
Posts: n/a
#2: Jan 25 '06

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;
Victor Bazarov
Guest
 
Posts: n/a
#3: Jan 25 '06

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
Markus Moll
Guest
 
Posts: n/a
#4: Jan 25 '06

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

mlimber
Guest
 
Posts: n/a
#5: Jan 25 '06

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

Victor Bazarov
Guest
 
Posts: n/a
#6: Jan 25 '06

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
Victor Bazarov
Guest
 
Posts: n/a
#7: Jan 25 '06

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!
codefuns
Guest
 
Posts: n/a
#8: Jan 25 '06

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.

Markus Moll
Guest
 
Posts: n/a
#9: Jan 25 '06

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

Bo Persson
Guest
 
Posts: n/a
#10: Jan 25 '06

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


Daniel T.
Guest
 
Posts: n/a
#11: Jan 26 '06

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.
Closed Thread


Similar C / C++ bytes