Say I have something like this
extern "C" void make_foo (char** tgt) {
*tgt = (char*) malloc(4*sizeof(char));
strncpy(*tgt, "foo", 4);
}
int main() {
char* foo;
make_foo(&foo);
std::string foos;
free(foo);
...
return 0;
}
Now, I would like to avoid using and then deleting the foo
buffer. I.e., I'd like to change the initialisation of foos
to something like
std::string foos;
and use no explicit free
.
Turns out this actually compiles and seems to work, but I have a rather suspicious feel about it: does it actually move the C-defined string and properly free the storage? Or does it just ignore the std::move
and leak the storage once the foo
pointer goes out of scope?
It's not that I worry too much about the extra copy, but I do wonder if it's possible to write this in modern move-semantics style.
Aucun commentaire:
Enregistrer un commentaire