How do we differentiate char arrays and string in c++? Is there anything char arrays do better than std::string ?
In C++ you should in almost all cases use std::string instead of a raw char array. std::string manages the underlying memory for you, which is by itself a good enough reason to prefer it.
Character arrays ( char[] ) can be cleared after use by setting each character to zero and Strings not. If someone can somehow see the memory image, they can see a password in plain text if Strings are used, but if char[] is used, after purging data with 0's, the password is secure.
String refers to a sequence of characters represented as a single data type. Character Array is a sequential collection of data type char. Strings are immutable. Character Arrays are mutable.
So the character array approach remains significantly faster although less so. In these tests, it was about 29% faster.
How do we differentiate char arrays and string in c++?
You don't, string literals are by definition null-terminated char arrays. Since arrays decay into pointers the first chance they get, const char*
is (still) often a synonym for string.
If you are asking about when you should write new char[n]
, the answer is never. If anything, it should be std::make_unique<char[]>(n);
and unless you are writing your own version of std::string
, use the standard one. If you need a buffer, use std::vector
or std::array
.
There are some advantages of const char[]
constants over const std::string
but they are being "solved" by the new C++ Standards:
Before C++20, std::string
could not be used in constexpr
context. So, I still prefer declaring global string constants with constexpr const char[]
if all I do is just passing them to some function. As @HolyBlackCat mentioned in the comments, C++17 std::string_view
makes this use-case obsolote too, especially with the new sv
literal:
#include <string_view>
using namespace std::literals;
//Compile-time string_view
constexpr auto str = "hello"sv;
const char*
is somewhat more universal. You can pass it to a function accepting const char*
, std::string
, or std::string_view
. The reverse requires std::string::c_str()
and it is not possible to so without copying the std::string_view
.
There is no dynamic allocation involved. Although std::string
might employ SSO, it is not guaranteed. This might be relevant for very small systems where the heap is precious and the program flash memory is more accomodating and contains the literal anyway.
Interacting with old libraries. But even then, std::string
is null-terminated too.
Overall, my recommendation would be to use std::string_view
every chance you get - for any non-owning string, including holding string literals. Most importantly, it should replace const char*
and const std::string&
function parameters. If you want to own a string, use std::string
.
One reason (which I personally don't think is a very good one) to use char arrays is the use case when you want your code to compile with both a C compiler and a C++ compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With