Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > delete() and new() on elements of an array created by new[]()

Reply
Thread Tools

delete() and new() on elements of an array created by new[]()

 
 
[rob desbois]
Guest
Posts: n/a
 
      04-15-2008
This may be a very dumb question, but brain can't seem to find
anything to confirm/deny suspicion.

I have an array created with:
FooClass* foo = new FooClass[numberOfFooObjects];

I want to be able to replace an element of that array at will with a
new FooClass.
Am I right in thinking that to delete foo[3], say, and replace with a
new FooClass, would require placement new? Would this be wise?

It seems that if I want to be able to replace elements at will with
newly constructed elements (rather than using the assignment
operator), that it would be better to do this:
FooClass** foo = new FooClass*[numberOfFooObjects];
Then create each object on the heap and store the pointers instead.
Replacing elements is easy and obvious then...

I feel I'm being really stupid here, can someone enlighten a poor
confuzzled coder please?
TIA
--rob
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      04-15-2008
[rob desbois] a écrit :
> I have an array created with:
> FooClass* foo = new FooClass[numberOfFooObjects];
>
> I want to be able to replace an element of that array at will with a
> new FooClass.
> Am I right in thinking that to delete foo[3], say, and replace with a
> new FooClass, would require placement new?


If indeed you can destroy the object
(foo+3)->~FooClass();
And then
new(foo+3) FooClass();

> Would this be wise?


Of course not.
It doesn't matter when you do it in raw memory but if you cannot
recreate the object (if it throws), then you have a big problem with how
to deal with your array when you want to destroy it.
(foo+3)->~FooClass();
try
{
new(foo+3) FooClass();
}
catch(...)
{
//
}
delete[] foo; //UB when destroying foo[3]


>
> It seems that if I want to be able to replace elements at will with
> newly constructed elements (rather than using the assignment
> operator), that it would be better to do this:
> FooClass** foo = new FooClass*[numberOfFooObjects];
> Then create each object on the heap and store the pointers instead.
> Replacing elements is easy and obvious then...


That or defining a void FooClass::swap(FooClass&)throw() method that
swap the two object.

Michael
 
Reply With Quote
 
 
 
 
[rob desbois]
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 1:50 pm, Michael DOUBEZ <(E-Mail Removed)> wrote:
> [rob desbois] a écrit :
>
> > I have an array created with:
> > FooClass* foo = new FooClass[numberOfFooObjects];

>
> > I want to be able to replace an element of that array at will with a
> > new FooClass.
> > Am I right in thinking that to delete foo[3], say, and replace with a
> > new FooClass, would require placement new?

>
> If indeed you can destroy the object
> (foo+3)->~FooClass();
> And then
> new(foo+3) FooClass();
>
> > Would this be wise?

>
> Of course not.
> It doesn't matter when you do it in raw memory but if you cannot
> recreate the object (if it throws), then you have a big problem with how
> to deal with your array when you want to destroy it.
> (foo+3)->~FooClass();
> try
> {
> new(foo+3) FooClass();}
>
> catch(...)
> {
> //}
>
> delete[] foo; //UB when destroying foo[3]


Ah yes, didn't think of that...


> > It seems that if I want to be able to replace elements at will with
> > newly constructed elements (rather than using the assignment
> > operator), that it would be better to do this:
> > FooClass** foo = new FooClass*[numberOfFooObjects];
> > Then create each object on the heap and store the pointers instead.
> > Replacing elements is easy and obvious then...

>
> That or defining a void FooClass::swap(FooClass&)throw() method that
> swap the two object.
>
> Michael


Great, thanks very much Michael
 
Reply With Quote
 
 
 
Reply

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
are the objects created in the stack guarranted to have been created? jimjim C++ 12 06-03-2005 12:57 PM
Auto-created Array elements? Brian Candler Ruby 2 10-12-2004 01:31 PM
Find if elements of one array are present in the other and return a boolean array Shalini C++ 2 01-09-2004 06:13 PM
schemalocation entry in XML created by castor created sources? Ulf Heyder XML 0 10-16-2003 02:14 PM



Advertisments