3 ms·
How does that even work on MSVC? Wouldn't it leak memory?
by dataflow 3y ago
How does that even work on MSVC? Wouldn't it leak memory?
- gavinsyancey 3y ago> How does that even work on MSVC? It sets the capacity to zero. MSVC then interprets it as a short string with length 0, which doesn't have an external allocation. > Wouldn't it leak memory? Yes, but I assume the "far more clever then smart" programmer didn't notice.
- dataflow 3y agoYeah I wouldn't call that working on MSVC either haha.
- gavinsyancey 3y agoDefinitely not "working", yeah, but On MSVC, an all-zero string object is the correct representation for a std::string containing "" (and the memory leak is easy to miss if you don't do it too much). On both Clang and gcc, an all-zero string object is not using the short-string optimization -- it is a string with size zero, capacity zero, and an external buffer of nullptr. (On clang this happens because capacity is even, and on gcc because &str->buf != &str). Any code that attempts to access its null terminator will dereference the null pointer and crash immediately.
- dataflow 3y agoYeah I get that.
- maccard 3y agoIf your string hadn't allocated yet then nope, probably not.