You can thank your favorite deity that this fail-safe is in, most functions aren't that kind.
In C, strings are null-terminated, which means they take an extra character than the actual data to mark where the string actually ends.
Since you're using C++ anyway, you should avoid bare-bones char arrays. Some reasons:
- buffer overflows. You managed to hit this issue on your first try, take a hint!
- Unicode awareness. We're living in 2015. Still using 256 characters is unacceptable by any standard.
- memory safety. It's way harder to leak a proper
string
than a plain old array. string
s have strong copy semantics that cover pretty much anything you can think of.
- ease of use. You have the entire STL algorithm list at your disposal! Use it rather than rolling your own.