0

How would I use delete on a specific class object in an array to ONLY delete that object and not the whole array?

This object will be no matter what be at the end of the array, so moving the other objects in the array doesn't matter also

EDIT:

To make it more clearer, I just want to free up the last element of the array for later use.

11
  • 2
    You shouldn't mutilate the array. You won't be able to call delete on it afterwards. Commented Feb 15, 2013 at 21:39
  • 3
    You can't do that (unless you agree to move everything except the last object to new array, and delete[] the old array). How about using std::vector instead? Commented Feb 15, 2013 at 21:40
  • 2
    Technically it is possible to do what you're asking for, but it would almost certainly not be what you mean. Commented Feb 15, 2013 at 21:41
  • 1
    What exactly are you trying to delete? Are you trying to delete a specific element in the array, or a member (pointer to instance) of a class in which the class is the member of the array? Post a code sample to clarify Commented Feb 15, 2013 at 21:42
  • I agree with @eladidan. What the OP is trying to achieve is unclear. Commented Feb 15, 2013 at 21:44

4 Answers 4

4

You should use std::vector instead of a regular array if you need to do this. With std::vector, removing and destroying the final element is done via the pop_back method.

If you absolutely must use a regular array, and must destroy one object inside the array right now, you can placement-destroy that object and then construct a new one in its place. If you aren't sure what this means or how to do it, you probably should be using std::vector instead.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to copy the last element of the array to somewhere else, simply do that and then ignore the last element. You cannot remove it. Usually you have a counter variable somewhere that holds the amount of elements currently in the array. So when you "remove" the last element, simply decrement that counter by 1.

FYI, std::vector works exactly the same way. (And you should be using std::vector to begin with anyway.)

Comments

1

An array allocated with the new[] directive can only be released with the delete[] directive. Any other attempt to release part of or the entire array (free, delete etc.) is undefined behaviour. If you want to be able to free a specific element in the array then you are better off looking into using an stl container such as std::vector, std::list, std::set, std::map etc. Each has different properties and is appropriate for a different task, but all support fast deletion of elements (except vector which only supports "fast" delteion of the last element but since that is exactly what you wanted then its a good option) and in fact "hide" the allocation of elements for you.

1 Comment

"Any other attempt... will fail." Actually, it may fail, or not to fail, or call your grandma
0

If the array is like

object* array = new object[n];

Then you cannot delete an element of it. You can move the elements by using memcpy(), but not change the size of the array.

Using new or delete on an array item will result in heap corruption in the majority of cases. As suggested, you should use std::vector.

If the array is an array of pointers to objects, then you can delete a pointed object but you cannot delete the pointer itself (see first part of this answer).

EDIT: a comment from the author of the question specify that he wants to remove the last element of a pointer array.

You can set the last pointer to 0 and check:

pointers[size - 1] = 0;
// later, when using the array
if(pointers[i]) // do smth

Using std::vector will be more easy. Also, if you deletions are frequent enough, you should consider using std::list.

Comments

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.