I'm a C programmer trying to write c++ code. I heard string
in C++ was better than char*
in terms of security, performance, etc, however sometimes it seems that char*
is a better choice. Someone suggested that programmers should not use char*
in C++ because we could do all things that char*
could do with string, and it's more secure and faster.
Did you ever used char*
in C++? What are the specific conditions?
It's safer to use
std::string
because you don't need to worry about allocating / deallocating memory for the string. The C++std::string
class is likely to use achar*
array internally. However, the class will manage the allocation, reallocation, and deallocation of the internal array for you. This removes all the usual risks that come with using raw pointers, such as memory leaks, buffer overflows, etc.Additionally, it's also incredibly convenient. You can copy strings, append to a string, etc., without having to manually provide buffer space or use functions like strcpy/strcat. With std::string it's as simple as using the
=
or+
operators.Basically, it's:
versus...
Edit:
In response to your edit regarding the use of
char*
in C++: Many C++ programmers will claim you should never usechar*
unless you're working with some API/legacy function that requires it, in which case you can use thestd::string::c_str()
function to convert anstd::string
toconst char*
.However, I would say there are some legitimate uses of C-arrays in C++. For example, if performance is absolutely critical, a small C-array on the stack may be a better solution than
std::string
. You may also be writing a program where you need absolute control over memory allocation/deallocation, in which case you would usechar*
. Also, as was pointed out in the comments section,std::string
isn't guaranteed to provide you with a contiguous, writable buffer *, so you can't directly write from a file into anstd::string
if you need your program to be completely portable. However, in the event you need to do this,std::vector
would still probably be preferable to using a raw C-array.* Although in C++11 this has changed so that
std::string
does provide you with a contiguous bufferShort answer, I don't. The exception is when I'm using third party libraries that require them. In those cases I try to stick to std::string::c_str().
C char * strings cannot contain '\0' characters. C++ string can handle null characters without a problem. If users enter strings containing \0 and you use C strings, your code may fail. There are also security issues associated with this.
In all my professional career I've had an opportunity to use std::string at only two projects. All others had their own string classes :)
Having said that, for new code I generally use std::string when I can, except for module boundaries (functions exported by dlls/shared libraries) where I tend to expose C interface and stay away from C++ types and issues with binary incompatibilities between compilers and std library implementations.
Implementations of
std::string
hide the memory usage from you. If you're writing performance-critical code, or you actually have to worry about memory fragmentation, then using char* can save you a lot of headaches.For anything else though, the fact that
std::string
hides all of this from you makes it so much more usable.std::string is almost always preferred. Even for speed, it uses small array on the stack before dynamically allocating more for larger strings.
However, char* pointers are still needed in many situations for writing strings/data into a raw buffer (e.g. network I/O), which can't be done with std::string.