What is placement new in C++ for and how to use it correctly?
Hello everyone! I'm learning C++ and came across something called placement new. In normal use, I use `new` to create objects on the heap and `new[]` for arrays. But there's this strange syntax: `new (pointer) Type`. I read that it allows creating an object in already allocated memory rather than allocating new memory. But why is this needed? Isn't it easier to always use regular `new`?\n\nFor example, I saw this code:\n```cpp\n#include // needed for placement new\n\nstruct MyStruct {\n int x;\n MyStruct(int val) : x(val) { std::cout << "Constructor"; }\n ~MyStruct() { std::cout << "Destructor"; }\n};\n\nint main() {\n void* buffer = std::malloc(sizeof(MyStruct));\n MyStruct* p = new (buffer) MyStruct(42); // placement new\n // ...\n p->~MyStruct(); // explicit destructor call