3 ms·
> replace "dumb" uses of std::string with essentially the equivalent of what a smart C programmer using pointers would write Can you give an example?
by edflsafoiewq 7y ago
> replace "dumb" uses of std::string with essentially the equivalent of what a smart C programmer using pointers would write
Can you give an example?
- asveikau 7y agoI am not op, but I do frequently feel like idiomatic C++ will create temporary copies of strings in areas where a C programmer would be less inclined to do so. For example, removing a prefix the C way would be to add the size of the prefix to the pointer. I guess c++ has string_views for that sort of case as of c++17.
- clappski 7y ago> For example, removing a prefix the C way would be to add the size of the prefix to the pointer. Surely to do this you'd still have to keep either a reference to the original pointer around or remember the offset so you could `free(str)` with the correct pointer?
- asveikau 7y agoYes, although it's not always your responsibility to do that. Firstly not all strings are on the heap, but ignoring that... If somebody passes you a pointer to a string and you aren't going to hold onto it for a while, you can just add to the pointer and it's no big deal. But yes, if you own the allocation, you need to keep the old pointer too.