2

I am not sure if both of these works to delete:

p = new int[1];

delete p

and

delete [] p;

If both works, what is the difference between the above two deletes?

1
  • 1
    The new and delete must match, or you have undefined behavior. Seeming to work on one platform is a possible outcome of undefined behavior, as is crashing everything terribly. Don't do it. Commented Mar 21, 2010 at 4:01

3 Answers 3

4

An array of one instance is still an array. There is simply not a special case, so you need to treat it as an array and not a single object. Non-array delete may crash.

An array of objects may be preceded by a number signifying the size of the array. If that's the case, delete[], not delete, is smart enough to pass the correct pointer to free.

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

Comments

2

They are not the same. Even though delete p would compile but could potentially cause problems at runtime.

delete p will invoke operator delete(void*) where as delete [] will call operator delete[](void*). The default behavior is for delete[] to call delete but if a custom operator for delete [] has been implemented, it won't be called if you just call delete and you'll have a problem (probably a silent one).

Good rule of thumb is if you used [] in the new, use it in the delete.

1 Comment

Not a rule of thumb, a requirement.
1

delete p deletes only one element. delete [] p deletes an array. If you use new type[], then use delete [].

If you're new has a [], then delete has a [].

By the way there has to be an exact dupliate of this somewhere.

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.