Using FFI to write a std::string to a buffer passed in from D
Johan
j at j.nl
Sun Jul 21 15:31:47 UTC 2024
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
More information about the Digitalmars-d-learn
mailing list