473,472 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
Jun 27 '08 #1
1 1104
On Apr 15, 1:50 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
[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 :-)
Jun 27 '08 #2

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

Similar topics

7
by: peter | last post by:
Hello, for a C++ delete: class Test{}; Test * A = new Test; delete A; We don't use delete A; How can C++ compiler can tell there are 50 destructor to deallocate?
8
by: Maximus | last post by:
Hi, I was wondering if it is bad to overuse the new & delete operator. In my application, e.g. I created my own list class and I will have to resize my variable maybe like 100 times during runtime...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
15
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
3
by: Brian Underhill via DotNetMonster.com | last post by:
I am trying to delete an element from an array. I have an array of 52 elements and I want to search for an element and delete it. Therefore, making it an array of 51 elements. is it just ...
27
by: fermisoft | last post by:
I have a strange problem in my project. I have a class like this...... class CMsg { public: void *m_body; ~CMsg()
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
9
by: Money | last post by:
If I allocate memory like this int *ptr = new int; Can I apply delete ptr; instead of delete ptr; since I am only allocating memory for 1 integer.
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.