2

Is it safe to delete every pointer in C++ as a pointer to an array?

Will it be fine for me if I will always write delete [] ptr with no regard what ptr actually is - whether it is a pointer to a single object or an array of objects?

ggghahaha
  • 55
  • 1
  • 8
  • You might just as well ask "Q: if I allocate an object with 'malloc()', can release it with 'delete'?" A: NO! Q: Why would you even ask such a question? Just to be arbitrary... or do you have an actual use case? – paulsm4 Apr 02 '18 at 22:11
  • 1
    @paulsm4 There are reasonable reasons to wonder this. Perhaps you want to understand how it works (it's reasonable to think that `new int` is perhaps equivalent to `new int[1]`), or perhaps you don't want to keep track of the details of how something was allocated. That's even more true in your `malloc`/`delete` example; if you interface with a C library you might actually have a hard time keeping track of which pointers came from where. – Daniel H Apr 02 '18 at 22:13
  • @paulsm4 I think the use case is: "I don't want to check how I allocated that object, whether with `new` or with `new []`, and I would like to have a universal `delete` that I can use in all situations" – Fabio says Reinstate Monica Apr 02 '18 at 22:14

1 Answers1

10

No, it won't be. Deleting something with delete [] that was not allocated with new [] produces undefined behaviour. And why would you want to? You allocated the memory, so you should know how to delete it. For further information, see http://en.cppreference.com/w/cpp/language/delete.

  • @wally see the cppreference page I added a link to. –  Apr 02 '18 at 22:09
  • 1
    It is safe to `delete[]` every pointer, as long as every pointer is allocated with `new[]`. Even if it is `new foo[1]`. Otherwise, no... it would crash the program on my machine. – Eljay Apr 02 '18 at 22:09
  • 1
    (Because my environment has separate heaps for `new` and `new[]` and `malloc`.) – Eljay Apr 02 '18 at 22:10
  • 1
    @wally [\[expr.delete\]p2](https://timsong-cpp.github.io/cppwp/n4659/expr.delete#2) – Daniel H Apr 02 '18 at 22:10
  • 6
    Actually, the statement isn’t entirely true: the `new` expression doesn’t need to mention `[]` to make `delete[]` necessary: `using array = int[10]; int* a = new array;`. – Dietmar Kühl Apr 02 '18 at 22:10
  • @Dietmar But isn't that array still allocated with `new[]`? –  Apr 02 '18 at 22:13
  • @NeilButterworth It's allocated with an [*array new-expression*](https://timsong-cpp.github.io/cppwp/n4659/expr.new#5), but syntactically it there are no square brackets in the vicinity of the keyword `new`, so it might be confusing. – Daniel H Apr 02 '18 at 22:15
  • Clearly not: there are no brackets in the *new expression*. Internally it will call `operator new[]()`, of course. However, the rule in the standard talks about *array objects* and not the syntax actually used. – Dietmar Kühl Apr 02 '18 at 22:16
  • Tricky! That is a good point! – Eljay Apr 02 '18 at 22:17
  • @Eljay Huh. I didn't know of any platforms that in practice didn't use `malloc` for `operator new`. I wouldn't expect `delete` and `delete[]` to be interchangeable in practice on any platform, because `delete[]` needs access to extra information `new` isn't allowed to store. – Daniel H Apr 02 '18 at 22:19
  • I'd reword the end of that last comment as "`new` isn't required to store." The implementation could store the book-keeping in cherry blossoms so long as it conforms to the standard. And yes, I would LOVE to see that computer. – user4581301 Apr 02 '18 at 22:31