Are C ++ Stripts variables mutable by UnLIKE Java Strings?

It is ok to manage strings in C ++ as follows:

string s = "Sting"; s[2] = 'a'; 

It works fine (and prints "Stang"), but is it safe to do this?

If so, does that mean they are mutable?

+6
source share
6 answers

C ++ string literals, i.e. something like "literal" immutable, although C ++ 03 allowed you to assign a pointer to such a literal on char* (this privilege was deprecated and removed for C ++ 11). Trying to change the character of a string literal is undefined behavior:

 char* s = "literal"; // OK with C++03; illegal with C++11 and later s[0] = 'x'; // undefined behavior 

C ++ std::string objects will certainly change if they are not declared as std::string const . If you think that the sequence of char objects is independent of each other, then you can assign individual objects. However, it is quite common that strings actually contain Unicode encoded as UTF-8 bytes. If the case in which any element of the string is changed can destroy the correct encoding, for example, because the continuation bit is replaced with something else.

So yes, strings are mutable, but can be semantically safe for assignment to individual elements.

+6
source

The string literal is immutable, as in Java.

String literals are stored in a read-only portion of memory.

For example, if you do something like the following, you will get segfault:

 char *ptr = (char *) "hello"; ptr[0] = 'a'; //segfault! you can't change something that immutable 

But! You can change the string with s[2] = 'a' , unless the string is declared with the const keyword,

The reason for this is that the = operator for strings is overloaded, takes a string literal as an argument, and then iterates over the string literal, copying each character into a mutable char array.

So, if you are comparing string literals in Java and C, they have the same behavior when it comes to immutability. String objects in Java and C also have the same behavior when it comes to change.

Here is an example showing that the string has a copy:

 #include <iostream> #include <string> using namespace std; int main() { const char *ptr = "hello"; string s = ptr; s[0] = 'b'; cout << s << endl; //prints bello cout << ptr << endl; //prints hello return 0; } 
+1
source

Yes and yes.

This is similar to changing the array at position 2.

0
source

Yes, it is absolutely safe and yes, the lines are mutable, which means that you can change them, add new things to them, and also remove parts from them.

0
source

Strings in C ++ are mutable, but with great power comes a big responsibility: you get undefined behavior if you read or store in string memory that goes beyond. For example, in your example, if you reference s [12], you will get undefined behavior.

0
source

C ++ std::string is mutable, and an assignment (usually) copies string data. The Java String immutable, and assignment copies the link to the string.

The type std::shared_ptr< const std::string > is somewhat similar to a Java string. However, this is a poor man. In C ++ 03, this was important for optimization, for example. sorting collections of strings, but with the C ++ 11 move semantics, it no longer has anything to do with it; ndash except academic pov.

0
source

Source: https://habr.com/ru/post/982296/


All Articles