2

I have a dynamic array (For this example, SIZE = 2):

polyTerm** polyn = new polyTerm* [SIZE];

I then add a few new ponlyTerm object to the array:

polyn[0] = new polyTerm(5.0,1);
polyn[1] = new polyTerm(2.0,1);

Now I want to remove the object from slot 0 and make the pointer null. How would I go about doing this? I currently have:

delete &polyn[0];

Then I use:

polyn[0] = NULL;

To make the pointer null.

Will this work?

EDIT: Correction, I need to use delete polyn[0] - even so, setting that to NULL should affect the pointer, as it would still technically point to the original location of the object. Resetting it to NULL removes any errors that could pop up later.

4
  • If you use delete[] it will call the destructors of polyTerm Commented Nov 4, 2013 at 17:02
  • I am aware of this, but that will delete ALL of the objects, correct? I want to remove only the object in slot 0. Commented Nov 4, 2013 at 17:03
  • it looks good to me. Is there any problem with it? Commented Nov 4, 2013 at 17:04
  • @lead_the_zeppelin It turns out it mostly worked, I had a small error elsewhere that was causing crashing. Took a while to find it! Commented Nov 5, 2013 at 13:49

1 Answer 1

2

The code you've posted is correct.

The individual object pointer, polyn[0] was created using the non-[] version of new, and so must be deleted using the non-[] version of delete -- which is what you are doing.

I'd still recommend avoiding all of this in the first place however, and using vector <unique_ptr <polyTerm> > instead. Then all you have to do is erase whatever elements you want, or clear the entire vector without having to worry about using the correct type and number of new and delete.


I'll demonstrate using a vector of smart pointers here, neatly avoiding the lack of make_unique (until C++14) by using shared_ptr instead.

// Create array (vector)
vector <shared_ptr <polyTerm>> polyn; 
// Populate array
polyn.push_back (make_shared (5.0, 1));
polyn.push_back (make_shared (2,0, 1));
// Remove 0th element from array
polyn.erase (polyn.begin());
Sign up to request clarification or add additional context in comments.

4 Comments

Sounds like it would work, though I am required to use array here. Just curious, would you mind showing my example, but with a vector, as I have little experience using them. Thanks!
@JeffB: Here's a demo.
Thanks! Just realized that I really need to use the array anyway, here is why: I am making a polynomial program, and I use the index in the array to signify the exponent. It is a little cheaty, but effective and works. Makes any algorithm that would add, multiply etc the polynomials much easier and efficient. (ie, polynomial with exponent 2 would be in slot 2)
@JeffB: vector has an operator[] overload which provides element access by index -- just like a regualr array. You don't need to use an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.