fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Foo
  5. {
  6. Foo(bool should_throw) {
  7. if(should_throw)
  8. throw std::logic_error("Constructor failed");
  9. cout << "Constructed at " << this << endl;
  10. }
  11. ~Foo() {
  12. cout << "Destroyed at " << this << endl;
  13. }
  14. };
  15.  
  16. void double_free_anyway()
  17. {
  18. Foo f(false);
  19. f.~Foo();
  20. new (&f) Foo(true);
  21. }
  22.  
  23. int main() {
  24. try {
  25. double_free_anyway();
  26. } catch(std::logic_error& e) {
  27. cout << "Error: " << e.what();
  28. }
  29. }
Success #stdin #stdout 0s 16056KB
stdin
Standard input is empty
stdout
Constructed at 0x7fff41ebf03f
Destroyed at 0x7fff41ebf03f
Destroyed at 0x7fff41ebf03f
Error: Constructor failed