new & delete



  • int* i = new int;
    delete (char*)i;
    

    Was passiert hier? Wird der gesammte allokierte Speicher fregegeben? Nur ein Byte? Eine Exception geworfen? ...?



  • Das dürfte undefiniertes Verhalten erzeugen.



  • Beleg dafür:

    <a href= schrieb:

    C++-Standard">[...] the value of the operand of delete shall be a pointer to a non-array object created by a new-expression, or a pointer to a sub-object representing a base class of such an object. If not, the behavior is undefined.

    [...] if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand's dynamic type and the static type shall have a virtual destructor or the behavior is undefined.


Log in to reply