3 ms·
For std::vector<int>, realloc would be fine. But you wouldn't want std::vector<std::string> to use realloc.
by usefulcat 11y ago
For std::vector<int>, realloc would be fine. But you wouldn't want std::vector<std::string> to use realloc.
- bluecalm 11y agoMy first reaction when I've read your comment was that it has to be because of sub-strings but as std::vector allocates memory again anyway that can't be too important. Can you elaborate a bit? I am not a C++ programmer but I am sometimes curious so maybe I am missing something obvious.
- animefan 11y agoI wad confused too but looking at the definition of realloc, it will sometimes allocate adjacent memory and sometimes allocate new memory and perform a move. In which case copying the std::string values might not be valid.
- bluecalm 11y agoWell, but std::vector always performs a move so any pointers to sub-strings won't be valid anyway from what I understand. I am probably just not getting something but it really seems like it should just use realloc. Maybe it has something to do with thread safety though as I don't think there are any guarantees for realloc.