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

前端 未结 7 876
粉色の甜心
粉色の甜心 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条回答
  •  萌比男神i
    2020-12-03 10:19

    Adding this since no other answer currently addresses it:

    Array delete[] cannot be used on a pointer-to-base class ever -- while the compiler stores the count of objects when you invoke new[], it doesn't store the types or sizes of the objects (as David pointed out, in C++ you rarely pay for a feature you're not using). However, scalar delete can safely delete through base class, so it's used both for normal object cleanup and polymorphic cleanup:

    struct Base { virtual ~Base(); };
    struct Derived : Base { };
    int main(){
        Base* b = new Derived;
        delete b; // this is good
    
        Base* b = new Derived[2];
        delete[] b; // bad! undefined behavior
    }
    

    However, in the opposite case -- non-virtual destructor -- scalar delete should be as cheap as possible -- it should not check for number of objects, nor for the type of object being deleted. This makes delete on a built-in type or plain-old-data type very cheap, as the compiler need only invoke ::operator delete and nothing else:

    int main(){
        int * p = new int;
        delete p; // cheap operation, no dynamic dispatch, no conditional branching
    }
    

    While not an exhaustive treatment of memory allocation, I hope this helps clarify the breadth of memory management options available in C++.

提交回复
热议问题