fork download
  1. #include <iostream>
  2. #include <cassert>
  3. using namespace std;
  4.  
  5. class Foo
  6. {
  7. public:
  8. Foo()
  9. {
  10. cout << "construct" << endl;
  11. }
  12. ~Foo()
  13. {
  14. cout << "destruct" << endl;
  15. }
  16. };
  17.  
  18. Foo* user(void* mem)
  19. {
  20. cout << " create" << endl;
  21. return new(mem) Foo();
  22. }
  23.  
  24. int main() {
  25.  
  26. void* mem=alloca(sizeof(Foo));
  27. assert(mem);
  28. Foo* foo=user(mem);
  29. cerr << "delete" << endl;
  30. foo->~Foo(); // нужно ЯВНО вызвать деструктор
  31. //а вот память, выделенную alloca, освобождать НЕ НУЖНО.
  32. return 0;
  33. }
Success #stdin #stdout #stderr 0s 3468KB
stdin
Standard input is empty
stdout
 create
construct
destruct
stderr
delete