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

Discussion in 'C++' started by [rob desbois], Apr 15, 2008.

  1. 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
     
    [rob desbois], Apr 15, 2008
    #1
    1. Advertising

  2. [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
     
    Michael DOUBEZ, Apr 15, 2008
    #2
    1. Advertising

  3. On Apr 15, 1:50 pm, Michael DOUBEZ <> 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 :)
     
    [rob desbois], Apr 15, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HeroOfSpielburg
    Replies:
    1
    Views:
    393
    Alf P. Steinbach
    Aug 6, 2003
  2. Jef Driesen
    Replies:
    1
    Views:
    504
    Gernot Frisch
    Jan 19, 2005
  3. X X
    Replies:
    4
    Views:
    358
    red floyd
    Jul 19, 2010
  4. Brian Candler

    Auto-created Array elements?

    Brian Candler, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    108
    Robert Klemme
    Oct 12, 2004
  5. Joe Buck
    Replies:
    11
    Views:
    190
    Robert Klemme
    Jan 2, 2010
Loading...

Share This Page