On Sunday, 21 July 2024 at 13:35:46 UTC, Troy wrote:
void create(void* b) { std::string s = "engineer again"; *(std::string*)(b) = s;// Segfault here }
You have to construct an empty string object first in location `b` (emplacement new). Then you can assign to it as you do. The `=` calls `operator=` which assumes that both operands (`b` and `s`) are both fully constructed and valid `std::string` objects. Without emplacement new, `b` is not a valid `std::string` object (random byte buffer returned by `malloc`).
Hope that works, Johan