4 ms·
It can lead to "fun" optimizations. I spent a while porting a codebase that was written by someone far more clever then smart. They had discovered that you cou
by fake-name 3y ago
It can lead to "fun" optimizations.
I spent a while porting a codebase that was written by someone far more clever then smart. They had discovered that you could clear a string under MSVC by `memset()`ing over it.
Obviously, this exploded in linux. That was a fun bug to track down.
- dataflow 3y agoHow 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.
- rewmie 3y ago> They had discovered that you could clear a string under MSVC by `memset()`ing over it. Why was that? Was it too problematic to just call std::string::clear ?