Is it possible to manually call the constructor of a class in C++?

c++

Solution

It sounds like you want to call the constructor an a piece of memory created by `malloc`. This is possible and is called placement new

void* pMemory = malloc(sizeof(C));
C* pValue = new (pMemory) C();

Problem

The most common way of creating an object of a class is by using the `new` keyword. It also calls the constructor. But if we used the `malloc` function to create the object, the constructor doesn't get called. Is it still possible to manually call the constructor after creating the object using `malloc`?

Original source