Unexpected C/C++ string behaviour
This is a fun one. Take the following mini C++ program:
#include <iostream> using namespace std; int main() { cout << "abcdefgh" + 0 << endl; cout << "abcdefgh" + 1 << endl; cout << "abcdefgh"
- 2 << endl; cout << "abcdefgh" + 3 << endl; cout << "abcdefgh" + 4 << endl; cout << "abcdefgh" + 8 << endl; return EXIT_SUCCESS; }
What happens? Well, at first glance you'd think that the output will be a list of concatenated strings like abcdefgh0
,
abcdefgh1
etc. After all, that's a pretty common idiom in just about every language that's more sophisticated than
PDP11 assembler... oh, hang on, C/C++ is PDP11 assembler in disguise. So these string literals won't behave like
std::string
--- they'll probably do something evil. And so it comes to pass:
buckley@d18:~/tmp/cc$ g++ -o teststradd teststradd.cc buckley@d18:~/tmp/cc$ ./teststradd abcdefgh
bcdefgh cdefgh defgh efgh (empty line)
I expected something evil, but this particular behaviour was a surprise: the first n characters are removed. What's
actually going on is that the string literal is a const char*
pointer, and the +n
is incrementing that pointer by
n units. The printout then starts at the offset and writes until it finds the terminating null character!
So much for the reason --- the main point is: WTF?!? One more reason to relegate C++ to purely system tasks like writing memory managers, device drivers and other bit-wrangling tools. Once strings, UIs and all that stuff get involved, we should be carefully segregating the C++ to places where it can't do any harm, and using Java, Python, D... or whatever for the touchy-feely stuff.