Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use char array instead of strings in c++?

How do we differentiate char arrays and string in c++? Is there anything char arrays do better than std::string ?

like image 838
Vettel Hamilton Avatar asked Dec 31 '20 18:12

Vettel Hamilton


People also ask

Should I use char array or 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.

Why is character array better than string?

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.

What is the difference between char array and string in C?

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.

Which is faster string or char array?

So the character array approach remains significantly faster although less so. In these tests, it was about 29% faster.


Video Answer


2 Answers

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:

  1. 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;
    
  2. 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.

  3. 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.

  4. 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.

like image 95
Quimby Avatar answered Sep 22 '22 06:09

Quimby


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.

like image 28
klutt Avatar answered Sep 19 '22 06:09

klutt