In my understanding for C++ memory model, only when object array is created by new[], and deleted by 'delete []', the scalar constructor/destructor are used, and compiler generates an internal for loop to iterate every element location.
int main()
{
B obj;
B* pb = new B;//no scalar constructor
delete pb;//scalar deleting destructor
}
But I found when I use 'new' 'delete' to operate just one element, without using '[]', VC still generates code for 'scalar deleting descturctor' for debug version.
So my question is:
- scalar constructor/destructor don't have to be appearing in pair, right? In my test program, I found there's only scalar deleting destructor.
I could be wrong but I don't think there is anything on the constructor side that is analogous to scalar deleting destructor.
- All objects created by
new
ornew[]
, should have a scalar deleting destructor? Why one element still have this consideration, I don't see any necessity for one element's case as to process exception, stack unwinding, that should rely on an extra deleting destructor. Any reasons?
delete ptr
calls the scalar deleting destructor.
delete [] ptr
calls the vector deleting destructor.
A very good answer is found at http://www.pcreview.co.uk/threads/scalar-deleting-destructor.1428390/.
It's the name that's reported for a helper function that VC writes for every class with a destructor. The "scalar deleting destructor" for class A is roughly equivalent to:
void scalar_deleting_destructor(A* pa) { pa->~A(); A::operator delete(pa); }
There's a sister function that's also generated, which is called the 'vector deleting destructor'. It looks roughly like:
void vector_deleting_destructor(A* pa, size_t count) { for (size_t i = 0; i < count; ++i) pa.~A(); A::operator delete[](pa); }