Pointer to const char vs char array vs std::string - c++

Here I've two lines of code
const char * s1 = "test";
char s2 [] = "test";
Both lines of code have the same behavior, so I cannot see any difference whether I should prefer s1 over s2 or vice-versa. In addition to s1 and s2, there is also the way of using std::string. I think the way of using std::string is the most elegant. While looking at other code, I often see that people either use const char * or char s []. Thus, my question is now, when should I use const char * s1 or char s [] or std::string? What are the differences and in which situations should I use which approach?

POINTERS
--------
char const* s1 = "test"; // pointer to string literal - do not modify!
char* s1 = "test"; // pointer to string literal - do not modify!
// (conversion to non-const deprecated in C++03 and
// disallowed in C++11)
ARRAYS
------
char s1[5] = "test"; // mutable character array copied from string literal
// - do what you like with it!
char s1[] = "test"; // as above, but with size deduced from initialisation
CLASS-TYPE OBJECTS
------------------
std::string s1 = "test"; // C++ string object with data copied from string
// literal - almost always what you *really* want

const char * s1 = "test";
char s2 [] = "test";
These two aren't identical. s1 is immutable: it points to constant memory. Modifying string literals is undefined behaviour.
And yes, in C++ you should prefer std::string.

The first one is constant, the second isn't. std::string is a class type and implements many useful functions and methods for string manipulation, making it much easier and user-friendly. The c-style 'strings' with char pointers are difficult to control, manipulate and often cause errors, but don't have the overhead the std::string has. Generally it's better to stick to the std::strings cause they're easier to maintain.

The only difference between the two that you should care about is this:
Which one is your project already using?

These two do not have the same behavior. s1 is a simple pointer which is initialized to point to some (usually read-only) area of the memory. s2, on the other hand, defines a local array of size 5, and fills it with a copy of this string.
Formally, you are not allowed to modify s1, that is, do something like s1[0] = 'a'. In particular, under weird circumstances, it could cause all other "test"s in your program to become "aest", because they all share the same memory. This is the reason modern compilers yell when you write
char* s = "test";
On the other hand, modifying s2 is allowed, since it is a local copy.
In other words, in the following example,
const char* s1 = "test";
const char* s2 = "test";
char s3[] = "test";
char s4[] = "test";
s1 and s2 may very well point to the same address in memory, while s3 and s4 are two different copies of the same string, and reside in different areas of memory.
If you're writing C++, use std::string unless you absolutely need an array of characters. If you need a modifiable array of characters, use char s[]. If you only need an immutable string, use const char*.

Use std::string unless you know why you need a char array / pointer to char.

which one to be used depends upon your requirement. Pointer offers you more flexiblity. and in some cases vulerability. Strings are a safe option and they provide Iterator support.

Related

C++ different behaviour when trying to assign to const types [duplicate]

Why does the following code in C work?
const char* str = NULL;
str = "test";
str = "test2";
Since str is a pointer to a constant character, why are we allowed to assign it different string literals? Further, how can we protect str from being modified? It seems like this could be a problem if, for example, we later assigned str to a longer string which ended up writing over another portion of memory.
I should add that in my test, I printed out the memory address of str before and after each of my assignments and it never changed. So, although str is a pointer to a const char, the memory is actually being modified. I wondered if perhaps this is a legacy issue with C?
You are changing the pointer, which is not const (the thing it's pointing to is const).
If you want the pointer itself to be const, the declaration would look like:
char * const str = "something";
or
char const * const str = "something"; // a const pointer to const char
const char * const str = "something"; // same thing
Const pointers to non-const data are usually a less useful construct than pointer-to-const.
Further, how can we protect str from being modified?
char * const str1; // str1 cannot be modified, but the character pointed to can
const char * str2; // str2 can be modified, but the character pointed to cannot
const char * const str3 // neither str3 nor the character pointed to can be modified.
The easiest way to read this is to start from the variable name and read to the left:
str1 is a constant pointer to a character
str2 is a pointer to a character constant
str3 is a constant pointer to a character constant
NOTE: the right-to-left reading does not work in the general case, but for simple declarations it's a simple way to do it. I found a java applet based on code from "The C Programming Language" that can decipher declarations with a full explanation of how to do it.
On a related note, definitely take a look at "const pointer versus pointer to const". It helps with what some people call const correctness. I keep it in my bookmarks so that I can refer to it every now and then.
What you're looking for may be the syntax...
const char* const str = NULL;
str = "test";
str = "test2";
Notice the "const" after the char* which yields a compiler error when trying to compile/build.
Memory for the string literals are allocated on the stack, and all your assignments do are change the str pointer to point to those memory addresses. The constant character it pointed to initially hasn't changed at all.
Besides, declaring a variable as const means that variable is read-only; it does not mean the value is constant!

What are the modifiable char array types in c++?

I am trying to synthesize my knowledge in char arrays but I feel I am missing something. I can only think of these three scenarios (when it comes to stack, I am not talking about doing const int SIZE = 6; char *word = new char[SIZE]; )
case 1 : not permissible
char *word = "message";
word[0] = 'M';
case 2 : permissible
const int SIZE = 8;
char word[SIZE] = "message";
word[0] = 'M';
case 3 : permissible
char word[] = "message";
word[0] = 'M';
Are there any others ways to create a char array in c++? If so what modifications are permissible?
If you declare the array with const type, you aren't allowed to modify it. If you declare it with non-const type, you are allowed to modify it. That's the main rule that you need to remember.
The rules of the language prevent you from accidentally breaking this rule using a pointer variable, as well. If you have a char*, you're allowed to use it to modify the array it points to. If you have a const char*, you're not. In the case of a non-modifiable array such as a string literal, the compiler should not allow you to assign its address to a char*. If the compiler allows Case 1 to compile, you should configure it with appropriate flags so that it does not.
In short, the modifiable array types are the non-const ones, and you can obtain a non-const pointer only to such an array, therefore such pointers can also be used for modification. (That is, unless you do something unsafe, like using const_cast).
Are there any others ways to create a char array in c++? If so what modifications are permissible?
Syntactically yes there are
Non Modifiable:
auto word1 = "abc";
const auto word2 = "abc";
constexpr auto word3 = "abc";
constexpr char word4 = "abc";
auto word5{"abc"};
const auto word6{"abc"};
Modifiable
char word7[]{"abc"};

C++ string literal type and declaration

Two types of the declaration:
char* str1 = "string 1";
and
char str2[] = "string 2";
my compiler doesn't allow me to use first declaration with a error
incorrect conversion from const char[8] to char*. looks okay,
the version like this:
const char* str1 = "string 1";
passed by compiler.
please clarify my understanding.
I believed that if we declare both versions e.g. in main(),
first one (const char*) - the only pointer will be allocated on the stack and initialized with some address in data segment.
second version (char[]) - whole array of symbols will be placed on the stack
as far as I see string literal does now always have a const char[] type.
is a using of const char* depricated? for C compatibility only?
where each version will store the string ?
char* str1 = "string 1";
This is deprecated as of C++98, and is ill-formed as of C++11, as a string literal can't be modified. Modifying it would result in undefined behavior.
To avoid this, the standard prohibits assigning it to a modifiable char pointer, as it might be modified later on without the programmer realizing that he/she shouldn't have modified it.
char str2[] = "string 2";
Yes, this allocated an array of characters, whereas each character is stored on the stack.
const char* str1 = "string 1";
This isn't deprecated, it is the recommended way (only way) to assign a string literal to a char pointer. Here, str1 is points to const chars, i.e. they can't be modified. Thus it is safe to use it somewhere, as the compiler will enforce that the chars will never be modified.
Here, str1 is stored on the stack, pointing to a string literal, which may or may not be stored in read-only memory (this is implementation defined).
char str2[] = "string 2";
"string 2" is string literal const char[9] stored in read-only memory.
char str2[] will allocate char array (of size deducted from initializer size) in read-write memory.
= will use the string literal to initialize the char array (doing memcpy of the "string 2" content).
I mean in principle. The actual machine code produced with optimizations may differ, setting up str2 content by less trivial means than memcpy from string literal.
char* str1 = "string 1"; - here you are trying to get the actual string literal memory address, but that one is const, so you shouldn't assign/cast it to char *.
const char* str1 should work OK, casting from const char[] to const char * is valid (they are almost the same thing, unless you have access to original array size during compilation, then the pointer variant is size-less dumb-down version).

in c++ is this a good practice to initialize char array with string literal?

in c++ is this a good practice to initialize char array with string?
such as:
char* abc = (char *) ("abcabc");
I see a lot of these in my co-worker's code. Should I change it to the right practice?
such as
std::string abc_str = "abcabc";
const char* abc= abc_str .c_str();
This statement
char* abc = (char *) ("abcabc");
is simply bad. String literals in C++ have types of constant character arrays. So a valid declaration will look like
const char *abc = "abcabc";
Note: In C you indeed may write
char *abc = "abcabc";
Nevertheless string literals are immutable. Any attempt to modify a string literal results in undefined behaviour.
By the way there is no any character array that is initialized by a string literal.:) Maybe you mean the following
char abc[] = "abcabc";
Using standard class std::string does not exclude using character arrays and moreover pointers to string literals.
Take into account that these declarations
const char *abc = "abcabc";
and
std::string abc_str = "abcabc";
const char* abc= abc_str .c_str();
are not equivalent. Relative to the first declaration string literals have static storage duration and their addresses are not changed during the program execution.
In the second declaration pointer abc points to dynamically allocated memory that can be reallocated if object abc_str will be changed. In this case the pointer will be invalid.
Also the first declaration supposes that the array (string literal) pointed to by the pointer will not be changed. In the second declaration it is supposed that the object of type std::string will be changed. Otherwise there is no great sense to declare an object of type std::string instead of the pointer.
Thus the meanings of the declarations are simply different.
char* abc = (char *) ("abcabc");
That is bad. Don't do it.
You are treating a string literal that is not supposed to be modified like it can be modified.
After that,
abc[0] = 'd';
will be OK by the compiler but not OK at run time. What you need to use is:
char abc[] = "abcabc";
This will create an array that is modifiable.
Both of those are bad.
char* abc = (char*) ("abcabc");
A string literal is a constant and, as such, may be stored in write protected memory. Therefore writing to it can crash your program, it is undefined behaviour.
Rather than cast away the constness you should keep it const and make a copy if you want to edit its contents.
const char* abc = "abcabc";
The other one should be avoided too:
std::string abc_str = "abcabc";
const char* abc = abc_str.c_str();
Keeping it const is good but if the string is changed it could be reallocated to another place in memory leaving your pointer dangling.
Also in pre C++11 code the pointer stops being valid the second it is assigned because there is no guarantee it is not a temporary.
Better to call abc_str.c_str() each time.
The chances are that because c_str() is such a trivial operation it will be optimized away by the compiler making it just as efficient as using the raw pointer.
Instead of both of those what you should be doing is using std::string all the way. If you absolutely need a const char* (for old legacy code) you can obtain it using c_str().
std::string abc_str = "abcabc"; // this is perfect why do more?
old_horrible_function(abc_str.c_str()); // only when needed

Pointers to char

char* pStr = new String("Hello");
char* s = "Hello";
Is the first one correct? Are there any difference between these two? My guess is that the first one is allocated on the heap,and the other one an the stack.Am i correct or are there any other differences?
The first one is just incorrect and won't compile because there is no such thing as String in either C or C++. The second one will compile, and is fine in C(afaik). In C++, however, the conversion from a string literal to char* is deprecated. You can unintentionally write later s[0] = 'X'; which is undefined behavior.
The correct way of doing it is using const (in C++)
const char * s = "Hello";
or, better, use string
std::string s("Hello");
pStr and s are pointers, so it is important to distinguish between the pointers themselves and the data that they point to.
On the first line, pStr is a pointer to an instance of the String class allocated on the heap. The string data inside this instance is a copy of a literal string "Hello" that is stored in the program's data segment. The copying is done by the String constructor. (You've referred to a String class, but I assume you mean std::string).
On the second line, s is a pointer to data stored in the program's data segment. Data in the data segment is immutable, so s should really be const char *.
There isn't enough information in your example to tell whether pStr and s are stored on the heap or the stack. If they are variables inside a function then they are on the stack. If there are members of a class then they are on the heap if the class was instantiated on the help (using new) or on the stack if it is instantiated as a value.
The line
char* pStr = new std::string("Hello");
will cause a compiler semantic error, because the LHS has a type of char* and the RHS has a type of std::string.
The line
char* s = "Hello"
will compile, but may give a warning, because the LHS has a type of char* and the RHS has a type of const char*.