2

If I allocate some raw memory with ::operator new, lets say 5*sizeof(int), is it undefined behavior if I call delete[] on the pointer?

I've been searching but didn't find anything regarding this besides that I should use delete[] on new[] and delete on new, same with operator new and operator delete.

Thanks in advance.

Mamma
  • 313
  • 2
  • 9
  • 1
    Specifically read [this answer](https://stackoverflow.com/questions/1913343/how-could-pairing-new-with-delete-possibly-lead-to-memory-leak-only/1931712#1931712) of that post. It is not simply a memory leak, it is fully undefined behavior. – Cory Kramer Oct 18 '15 at 17:39
  • perhaps I should have added "doing it on built in types". But thank you for the answer mate. – Mamma Oct 18 '15 at 17:42
  • @CoryKramer Pardon me, but I don't believe this is a dup. Question in link asks for the reverse thing (i.e., allocating with `new[]` and freeing with `delete`). Plus, the answers although good don't have a reference to the standard. – 101010 Oct 18 '15 at 17:53
  • According to the draft standard [N4527](http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4527.pdf) *5.3.5/p2 Delete [expr.delete]*: ... the value of the operand of delete may be a null pointer value, a pointer to a non-array object created by a previous new-expression, or a pointer to a subobject (1.8) representing a base class of such an object (Clause 10). **If not, the behavior is undefined.** ... – 101010 Oct 18 '15 at 17:59
  • Try this for a duplicate then: [Is `delete[]` equal to `delete`?](http://stackoverflow.com/questions/1553382/is-delete-equal-to-delete) – Bo Persson Oct 18 '15 at 18:10

0 Answers0