Why does the compiler require `delete [] p` versus `delete p[]`?
Asked Answered
J

2

16

In C++, if you want to dynamically allocate an array, you can do something like this:

int *p;
p = new int[i];  // i is some number

However, to delete the array, you do...

delete[] p;

Why isn't it delete p[]? Wouldn't that be more symmetrical with how it was originally created? What is the reason (if there is any) why the language was designed this way?

Juncaceous answered 26/6, 2012 at 7:4 Comment(5)
Why the downvote/vote to close? What can I do to improve the question?Juncaceous
@Deflect: That was mine, Usually 'why was this language construct chosen?' questions are simply not constructive because there is no objective, reasonable answer. However, in this case there are a few decent reasons, so I may have judged too soon (I removed my downvote quite quickly).Wilden
@KillianDS: Okay, that makes sense. Thanks for replying! [and reassuring me that I'm not totally off-base :)]Juncaceous
@KillianDS: Well, then why not just answer by "there's no serious reason, other languages do this and this and it just happened that C++ goes its way"?Chequerboard
I don't know how useful this is, but this is the way I'd read the code in English: p is an integer pointer; p is a new integer array of length i; delete the array p.Trela
R
29

One reason could be to make these cases more distinct.

int ** p;
delete[] p
delete p[1];

If it were delete p[] then a one character error would have pretty nasty consquences.

Rebate answered 26/6, 2012 at 7:8 Comment(3)
This is especially true, in that in early C++, you had to specify the number of elements in the [].Airtoair
@JamesKanze: Sounds like that's the answer to me. delete p[4] would be ambiguous.Idolum
delete []p[1]; is also valid, and compiler must know the programmer's intent!Cribble
V
2

Because array decays to pointer when passing as parameter to function (or operator). So delete p[] would be simply equivalent to delete p.

[edit] We may think of delete as of special template operator. The operator should be able to distingish between p and p[] to pick the right "specialization" (non-array or array deletion). However, template argument deduction rules make this choice impossible (due to array decaying we can't distingish between p[] and p when deducing the argument).

So we can't use operator with name delete for both casees and need to introduce another operator delete[] with diffrent name (suffix [] can be treated as part of operator's name) for array case.

[edit 2] Note. delete p[] is not valid sintax at all according to the current standard. The reasoning above only shows problems that could araise if we would try to interpret delete p[] using existing c++ concepts.

Vashtivashtia answered 26/6, 2012 at 9:51 Comment(3)
No, it is not equivalent. Using one in place of another is undefined behavior.Chequerboard
@sharptooth: ? passing an array into templated function instead of pointer is UB? I think about delete as about templated operator and this operator can't pick the right specialization because it can't distingish between p[] and p when deducing the argument. So we need to introduce a special operator delete[] for arrays instead of using the same operator nameVashtivashtia
@sharptooth: of course, but it's unrelated to my answer. I said only that delete p and delete p[] are equivalent statements (perhaps, both expose UB if p is an array created by new int[i])Vashtivashtia

© 2022 - 2024 — McMap. All rights reserved.