Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you prefer char* instead of string, in C++?

Tags:

c++

c

string

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?

like image 472
Jichao Avatar asked Nov 19 '09 15:11

Jichao


People also ask

Should I use char * or string?

There is no reason to use char* when you need a string -- unless your primitive strings are fixed on the stack or compiled in and the profiler said that's how they should be. Advising to use a char* over a string is roughly never a good advice if the profiler didn't say the same thing.

Why is char used instead of string?

The main reason for using const char* is because it is compatible and linkable with C. Many APIs and other libraries have C interfaces (you use the API via a collection of C-like functions).

Is char * the same as string?

The difference between a string and a char* is that the char* is just a pointer to the sequence. This approach of manipulating strings is based on the C programming language and is the native way in which strings are encoded in C++.

What is the advantage of using a char pointer as a string instead of taking an array of characters?

The one advantage that I'm seeing is that with char * it is easier to distinguish "no string provided" and "empty string provided", also it occupies less space then an array. Also, in both cases I can initialize: struct my_struct p { . p = 10, .


2 Answers

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 a char* 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:

 std::string s1 = "Hello ";
 std::string s2 = s1 + "World";

versus...

 const char* s1 = "Hello";
 char s2[1024]; // How much should I really even allocate here?
 strcpy(s2, s1);
 strcat(s2, " World ");

Edit:

In response to your edit regarding the use of char* in C++: Many C++ programmers will claim you should never use char* unless you're working with some API/legacy function that requires it, in which case you can use the std::string::c_str() function to convert an std::string to const 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 use char*. 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 an std::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 buffer

like image 90
Charles Salvia Avatar answered Sep 19 '22 05:09

Charles Salvia


Ok, the question changed a lot since I first answered.

Native char arrays are a nightmare of memory management and buffer overruns compared to std::string. I always prefer to use std::string.

That said, char array may be a better choice in some circumstances due to performance constraints (although std::string may actually be faster in some cases -- measure first!) or prohibition of dynamic memory usage in an embedded environment, etc.

like image 35
Fred Larson Avatar answered Sep 19 '22 05:09

Fred Larson