fork download
  1. #include <iostream>
  2.  
  3. struct Base
  4. {
  5. Base() { std::cout << "Base" << std::endl; }
  6. virtual ~Base() { std::cout << "~Base" << std::endl; }
  7. int i;
  8. };
  9.  
  10. struct Derived : public Base
  11. {
  12. Derived() { std::cout << "Derived" <<std::endl; }
  13. virtual ~Derived() { std::cout << "~Derived" << std::endl; }
  14. int it[10]; // sizeof(Base) != sizeof(Derived)
  15. };
  16.  
  17. int main()
  18. {
  19. Base *bp = new Derived;
  20. Base *bq = new Derived[5];
  21.  
  22. delete bp;
  23. delete[] bq; // this causes runtime error
  24. return 0;
  25. }
Runtime error #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
Base
Derived
Base
Derived
Base
Derived
Base
Derived
Base
Derived
Base
Derived
~Derived
~Base