473,378 Members | 1,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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?

Jan 25 '06 #1
10 1686
Hi

codefuns wrote:
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?


By a pseudo-destructor call.

T object;
Jan 25 '06 #2
codefuns wrote:
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?


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
Jan 25 '06 #3
Hi

codefuns wrote:
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?


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

Jan 25 '06 #4
codefuns wrote:
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?


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

Jan 25 '06 #5
Markus Moll wrote:
codefuns wrote:

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?

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


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
Jan 25 '06 #6
mlimber wrote:
codefuns wrote:
[..]?

See these FAQs:
[..]


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!
Jan 25 '06 #7
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.

Jan 25 '06 #8
Hi

Victor Bazarov wrote:
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.


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

Jan 25 '06 #9

"Victor Bazarov" <v.********@comAcast.net> skrev i meddelandet
news:D3***************@newsread1.mlpsca01.us.to.ve rio.net...

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

:-)

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.


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
Jan 25 '06 #10
In article <11**********************@g49g2000cwa.googlegroups .com>,
"codefuns" <co******@gmail.com> wrote:
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 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.
Jan 26 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: billnospam | last post by:
Is it possible to overload operators in vb.net? Is it possible to do programmer defined boxing on byvalue variables in vb.net?
5
by: Gonçalo Rodrigues | last post by:
Hi all, Sorry to bother you guys again with a template question but I'm really trying to understand them and there's some piece of info that I'm missing. I'm sure I'm being really dumb here so,...
2
by: grahamo | last post by:
Hi, I realise that c++ knows nothing about threads however my question is related to an (excellent) article I was reading about threads and C++. For all intents and purposes we can forget the...
5
by: Riku Jarvinen | last post by:
Hi everyone, I asked another question regarding this same subject about a week ago. See thread: ...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
15
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
10
by: Jeroen | last post by:
Hi guys, Just another question. Suppose I have 2 classes (incomplete code): class A { A(const B& b); A& operator = (const A& a); }; class B {
2
by: sven.bauer | last post by:
Hi, I have a question following up the following slightly older posting: http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e52371e89806ae/52a3a6551f84d38b class Base {...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.