Say I am given a long and null-terminated cstring as char* text_ptr
. I own text_ptr
and I am responsible of free()
ing it. Currently, I use text_ptr
and free()
it each time after use.
I try to improve memory safety a bit by wrapping it in a C++ class so that I can enjoy the benefit of RAII. There could be many ways to achieve it. A naive way is: string text_ptr(text_ptr);
. However, by doing so, memory is copied once and I still need to manually free()
my text_ptr
. It would be better if I can avoid memory copy and free()
(as this text_ptr
is created frequently, performance could take a big hit if I copy it each time). My current thought:
Is it possible to transfer the ownership of text_ptr
to a string text_str
? Hypothetically, I do text_str.data() = text_ptr;
.
Thanks