What is the Time Complexity of the delete[]
operator?
The amount of the time required is of course implementation defined. However, the operator applies only to the pointer to the 1D array and thus it's O(1).
I mean how is it implemented - does it iterate over all the elements
in the array and calls destructor for every element?
Yes.
Provided that it's called only on the exact pointer which is assigned a memory created using new[]
. For primitive types there are no user defined destructors.
Does this operator do the same for primitive types (int, etc.) and
user defined types?
The comparison is not fair, because primitive types don't have user defined destructor and they cannot be polymorhpic.
For example, delete[]
on a polymorphic class is an undefined behavior. i.e.
Base* p1 = new Derived, *p2 = new Derived[2];
delete p1; // ok
delete[] p2; // bad