Why do we even need the “delete[]” operator?

前端 未结 7 862
粉色の甜心
粉色の甜心 2020-12-03 10:02

This is a question that\'s been nagging me for some time. I always thought that C++ should have been designed so that the delete operator (without brackets) wor

相关标签:
7条回答
  • 2020-12-03 10:34

    It's so that the destructors of the individual elements will be called. Yes, for arrays of PODs, there isn't much of a difference, but in C++, you can have arrays of objects with non-trivial destructors.

    Now, your question is, why not make new and delete behave like new[] and delete[] and get rid of new[] and delete[]? I would go back Stroustrup's "Design and Evolution" book where he said that if you don't use C++ features, you shouldn't have to pay for them (at run time at least). The way it stands now, a new or delete will behave as efficiently as malloc and free. If delete had the delete[] meaning, there would be some extra overhead at run time (as James Curran pointed out).

    0 讨论(0)
提交回复
热议问题