fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class my {
  6. public:
  7. int x;
  8.  
  9. my() {
  10. x = 0;
  11. std::cout << "constructor called" << std::endl;
  12. }
  13.  
  14. ~my() {
  15. std::cout << "destructor called" << std::endl;
  16. }
  17. };
  18.  
  19. int main() {
  20. auto mem = malloc(sizeof(my));
  21. if (mem) {
  22. auto myPtr = new(mem) my;
  23.  
  24. myPtr->~my();
  25. free(mem);
  26. }
  27. }
  28.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
constructor called
destructor called