Why is it undefined behavior to delete[] an array of derived objects via a base pointer?

Base* p = new Base[n] creates an n-sized array of Base elements, of which p then points to the first element. Base* p = new Derived[n] however, creates an n-sized array of Derived elements. p then points to the Base subobject of the first element. p does not however refer to the first element of … Read more

How to delete void pointer?

This as written is legal. The cast back to MyCls* is critical. Without that, you will invoke undefined behavior–the MyCls destructor will not be called, and other problems may arise as well (such as a crash). You must cast back to the correct type. Also note that this can be complicated if multiple inheritance is … Read more

Time complexity of delete[] operator [duplicate]

::operator delete[] is documented on cplusplus.com (which is sometimes frowned upon) as: operator delete[] can be called explicitly as a regular function, but in C++, delete[] is an operator with a very specific behavior: An expression with the delete[] operator, first calls the appropriate destructors for each element in the array (if these are of … Read more

Why doesn’t GCC optimize out deletion of null pointers in C++?

According to C++14 [expr.delete]/7: If the value of the operand of the delete-expression is not a null pointer value, then: [ …omitted… ] Otherwise, it is unspecified whether the deallocation function will be called. So both compilers do comply with the standard, because it’s unspecified whether operator delete is called for deletion of a null … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)