fork download
  1. #include <iostream>
  2. #include <memory.h>
  3.  
  4. struct B {
  5. B() { std::cout << "B()" << std::endl; }
  6. ~B() { std::cout << "~B()" << std::endl; }
  7. };
  8.  
  9. int main()
  10. {
  11. std::cout << "start main" << std::endl;
  12. { // scope
  13. std::cout << "start scope" << std::endl;
  14. B b;
  15. std::cout << "end scope" << std::endl;
  16. } // end scope, b gets auto-destroyed.
  17. std::cout << "end of first example." << std::endl << std::endl ;
  18.  
  19. { // scope
  20. std::cout << "start scope" << std::endl;
  21. void* stackStorage = alloca(sizeof(B));
  22. std::cout << "alloca'd" << std::endl;
  23. B* b = new (stackStorage) B();
  24. std::cout << "ctord" << std::endl;
  25. b->~B(); // <-- we're responsible for dtoring this object.
  26. std::cout << "end scope" << std::endl;
  27. } // <-- b gets destroyed here, but it's just a pointer.
  28. std::cout << "end main" << std::endl;
  29. }
  30.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
start main
start scope
B()
end scope
~B()
end of first example.

start scope
alloca'd
B()
ctord
~B()
end scope
end main