int* p = new int[10]; delete p; is executed. What is the defect?
Correct answer
B. The form of delete does not match the form of new, so the behaviour is undefined
15 views
B. The form of delete does not match the form of new, so the behaviour is undefined
15 views
Array new records the element count in a cookie that delete[] uses to run every destructor, so pairing new[] with scalar delete is undefined behaviour and frequently corrupts the allocator. With int the destructors are trivial, so the corruption may stay hidden in testing. The reverse mismatch, delete[] applied to a scalar new, is equally fatal.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.