Using FFI to write a std::string to a buffer passed in from D

Troy tmj9001 at gmail.com
Sun Jul 21 13:35:46 UTC 2024


I have a weird issue I've been running into while trying to write 
D bindings to some C++ code.  I'm trying to call some C++ 
functions that take in a std::string but don't make a copy of it. 
  To do this, I want to write a std::string to a buffer passed in 
from D so I can keep it from being destructed.

However, I'm getting a segfault when I attempt to write to the 
buffer I pass in despite supposedly allocating enough memory to 
store a std::string.  Even stranger, when I allocate 488 instead 
of 32, the segfault goes away.

Below, I've created a minimal example that illustrates my problem:
**main.d**
```d
import core.stdc.stdlib;

extern(C++) {
void create(void*);
void display(void*);
}

void main() {
	void* buf = malloc(32);// The size of a std::string on my 
platform is 32.
	//^--strangely, below segfault doesn't happen when 488 is 
allocated instead
	create(buf);
	display(buf);
}
```

**dothings.cpp**
```c++
#include <string>
#include <fstream>

void create(void* b) {
	std::string s = "engineer again";
	*(std::string*)(b) = s;// Segfault here
}

void display(void* b) {
	std::string s = *(std::string*)(b);
	std::ofstream file;
	file.open("imwishing");
	file << s << "\n";
	file.close();
}
```
(To build, I call "gdc main.d dothings.cpp -lstdc++ -g")

I suspect I'm making some false assumptions somewhere along the 
line, so please feel free to ask if you'd like to know more about 
what I'm doing here.


More information about the Digitalmars-d-learn mailing list