Is it safe to call free in the example below:
size_t len = 10;
char* buffer = static_cast<char*>(malloc(len));
std::string_view sview(buffer, len);
free(sview.data())
Why do I need this?
I have a mix of C++ and C code. I am using custom malloc and free. I want to use std::string_view instead of char* for the sake of STL containers, but I am also responsible for freeing the data.
free()must be equal to the value returned frommalloc(). In practicesview.data()will be just that. But I'm ont sure it is guaranteed.std::stringfor the actual string ?std::basic_stringhas possibility to use custom allocator. Else you can still have your ownmy_string, and usestd::string_viewfor non-owning accesses.mallocandfree. I want to usestd::string_viewinstead ofchar*for the sake of STL containers, but I am also responsible for freeing the data" - you can usestd::unique_ptrwith a custom deleter to own and free the data. Let thestring_viewbe just a view into the data, nothing more.