fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <new>
  4.  
  5. struct Example_Class
  6. {
  7. char c_style_array[20];
  8. int number;
  9. std::string normal_string;
  10. Example_Class()
  11. : number(7)
  12. , normal_string("Hello, placement new!")
  13. {
  14. }
  15. };
  16.  
  17. int main()
  18. {
  19. unsigned char my_buffer[sizeof(Example_Class)];
  20. Example_Class *p = new (my_buffer) Example_Class;
  21. std::cout << p->number << std::endl;
  22. std::cout << p->normal_string << std::endl;
  23. p->~Example_Class();
  24. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
7
Hello, placement new!